Laravel function with combination in pivot table - php

Hello I have problem with my requests. How can I make that functions wont't return data which exist in second one. ex. user_id = 1 friend_id = 2 from sendedRequests and user_id = 2, friend_id = 1 from pendingRequests.
Here's the code :
public function showSendedRequests(){
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where('who_send', $this->id)
->where('user_id', $this->id)
->where('accepted',false)
->get();
return $request;
}
public function showPendingRequests(){
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',false],['who_send','!=',Auth::user()->id]])
->get();
return $request;
}

You can do something like this, using the <> not equal
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where('who_send', $this->id)
->where('user_id','<>', $this->id)
->where('accepted',false)
->get();
return $request;
}
//for the next function
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',false],['who_send','<>',Auth::user()->id]])
->get();
return $request;
}
For further reference

Related

Laravel - Method Illuminate\\Support\\Collection::makeHidden does not exist

I want to hide the columns password & OTP ,that is included in $uses result. Actually these 2 columns are part of the users table. I've tried like below. But it generates the error - Method Illuminate\\Support\\Collection::makeHidden does not exist . How to solve this? Any suggestions..
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$d=$users->makeHidden(['password','OTP']);
return response()->json([
'message' => 'profile viewed successfully',
'data' => $d,
'statusCode' => 200,
'status' => 'success'],200);
You're trying to execute this method on the collection but it's a model method:
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
foreach($users as $user) {
$user->makeHidden(['password','OTP']);
}
And this still doesn't work since you're using DB::table('users') over Users::all().
In order to use a model, you have to do the following:
model:
class User extends Model
{
// Instead of `makeHidden()` you can do this if you want them always to be hidden
// protected $hidden = ['password','OTP'];
public function location()
{
return $this->hasOne(App\Models\Location::class, 'users.id', '=', 'location.id');
}
public function technical_details()
{
return $this->hasOne(App\Models\UserTechnicalDetail::class, 'users.id', '=', 'user_technical_details.id');
}
}
controller:
$users = Users::with(['location', 'technical_details'])
->get();
foreach($users as $user) {
$user->makeHidden(['password','OTP']);
}
Simplest solution for your case:
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get()
->map(function ($user) {
unset($user->password);
unset($user->OTP);
reurn $user;
});
I'd recommend to use Eloquent Relationships instead of joins for better abstraction:
https://laravel.com/docs/8.x/eloquent-relationships
In order to get User object from db, you should use User Model:
$users = User::query()
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
foreach($users as $user) {
$user->makeHidden(['password','OTP']);
}
I'm late to the party, sorry.
I had the same issue. I wanted to exclude created_at and updated_at columns from my SQL result (using DB:Table, not specific eloquent models). The makeHidden function "does not exist". NoOorz24 has a similar answer to mine, but instead, I did this (which is useful if you're running a slightly old version of PHP). It's a bit more processor hungry but it did the job.
$content = DB::table($talbename)->get();
foreach($content as $c) {
unset($c->created_at);
unset($c->updated_at);
}

Laravel query id based array

I have such results
Bur when I try to get results of different table based on this data (screenshot) ids it says
Property [id] does not exist on this collection instance
Code
$result = DB::table('zone_regions')
->where('zone_regions.id', '=', $request->input('zone'))
->join('areas', function ($join) use($area) {
$join->on('zone_regions.id', '=', 'areas.zone_id')
->where('areas.id', '=', $area);
})
->join('hthree_regions', function ($join) use($city) {
$join->on('areas.id', '=', 'hthree_regions.area_id')
->where('hthree_regions.id', '=', $city);
})
->join('segments', function ($join) use($segment) {
$join->on('hthree_regions.id', '=', 'segments.hthree_id')
->where('segments.id', '=', $segment);
})
->join('links', function ($join) use($link) {
$join->on('segments.id', '=', 'links.segment_id')
->where('links.id', '=', $link);
})
->join('titik_closurs', function ($join) use($closure) {
$join->on('links.id', '=', 'titik_closurs.link_id');
})
->groupBy('titik_closurs.id')
->get();
$histories = DB::table('core_histories')->whereIn('titik_id', $result->id)->get(); //get histories of returned results (return error above)
$res = array_merge($result, ['histories' => $histories]);
Any idea?
Try this
$histories = DB::table('core_histories')->whereIn('titik_id', $result->pluck('id')->toArray())->get();
https://laravel.com/docs/7.x/collections#method-pluck

How to call all variable from other method and return it in same view

I need help. I want to pass all variabel, call in index, and return in same view. How to call perFolder method and call the variabel in perFolder method such as userj.
class fileCount extends Controller {
public function index() {
$fj = $this->perFolder();
$users = DB::table('files')->where('created_by_id', '=', '1')->count();
$users2 = DB::table('files')->where('created_by_id', '=', '2')->count();
$users3 = DB::table('files')->where('created_by_id', '=', '3')->count();
$users4 = DB::table('files')->where('created_by_id', '=', '4')->count();
$users5 = DB::table('files')->where('created_by_id', '=', '5')->count();
$users6 = DB::table('files')->where('created_by_id', '=', '6')->count();
$users7 = DB::table('files')->where('created_by_id', '=', '7')->count();
$users8 = DB::table('files')->where('created_by_id', '=', '8')->count();
$users9 = DB::table('files')->where('created_by_id', '=', '9')->count();
$users10 = DB::table('files')->where('created_by_id', '=', '10')->count();
$users71 = DB::table('files')->where('created_by_id', '=', '11')->count();
//$row_count=DB::table('media')->where('file_name', '>', '1')->count();
$row_count = DB::table('media')
->select('file_name', DB::raw('COUNT(*) as `count`'))
->groupBy('file_name')
->havingRaw('COUNT(*) > 1')
->get();
//$row_count = DB::table('media')->where('file_name', '=', $file_name)->count();
return view('admin.report.index2', compact('users', 'users2', 'users3', 'users4', 'users5', 'users6', 'users7', 'users8', 'users9', 'users10', 'users71', 'row_count'));
}
public function perFolder() {
$userj = DB::table('files')->where('created_by_id', '=', '1')->where('folder_id', '=', '1')->count();
$userj2 = DB::table('files')->where('created_by_id', '=', '2')->where('folder_id', '=', '2')->count();
return view('admin.report.index2', compact('userj','userj2'));
}
}

Function or Variable in Where Statement?

Is there a way to put a function or variable in the first value of a where statement?
For example,
$equipments=DB::table('equipments')
->leftjoin('orders', function ($join) {
$join->on('orders.equipment_id', '=', 'equipments.equipmentID')
->where('orders.orderUser_id','=', Auth::user()->id);
})
->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
->where('equipments.equipmentArchived','=','0')
*->where($this->countOrders('equipments.equipmentID'),'<','1')*
->orderBy('equipments.equipmentListedDate','DESC')
->distinct()
->paginate(10);
The count orders function is
public static function countOrders($id)
{
//
$members=DB::table('orders')
->leftjoin('users', 'users.id', '=', 'orders.orderUser_id')
->leftjoin('equipments', 'equipments.equipmentID', '=', 'orders.equipment_id')
->where('orders.equipment_id','=', $id)
->where('orders.status','=','ACCEPTED')
->count();
return $members;
}
You can use "whereRaw" method to do that.
$equipments = DB::table('equipments')
->leftjoin('orders', function ($join) {
$join->on('orders.equipment_id', '=', 'equipments.equipmentID')
->where('orders.orderUser_id','=', Auth::user()->id);
})
->leftjoin('business_users', 'business_users.id', '=' , 'equipments.user_id')
->where('equipments.equipmentArchived','=','0')
->whereRaw('? < ?', [$this->countOrders('equipments.equipmentID'), 1])
->orderBy('equipments.equipmentListedDate','DESC')
->distinct()
->paginate(10);

Display data from left join query [Laravel 5]

stackers
I'm facing a problem on displaying data from query that contain join statement.
public function view(Request $id)
{
$user_id = $id->input('id');
$users = DB::table('users')->select('*')
->leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
->leftjoin('roles', 'roles.id', '=', 'role_user.role_id')
->where('users.id', '=', $user_id)->get();
return view('/admin/view_user', ['users' => $users]);
}
From the above code, how do i display the row and the join table data from roles, and role_user
Try This :
public function view(Request $id)
{
$user_id = $id->input('id');
$users = DB::table('users')
->select('users.*','role_user.fieldName','roles.fieldName')
->leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
->leftjoin('roles', 'roles.id', '=', 'role_user.role_id')
->where('users.id', '=', $user_id)->get();
return view('/admin/view_user', ['users' => $users]);
}
Write tableName.fieldName to get perticalur table's data in JOIN query

Categories