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
Related
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);
}
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
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);
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
I have a method which returns a list of provinces based on some join and where clauses. The problem I have is That laravel throws an Exception that tells there is an undefined method called whereIn. But there exist whereIn method in Eloquent. How should I use whereIn method on joins?
public static function getProvinces($IDs = array()){
$query =
DB::Table('doctors')
->join('users', function($join){
$join->on('users.id', '=', 'doctors.user_id')
->whereIn('users.status_id', array(3,4,7));
})
->join('contacts', function($join){
$join->on('doctors.id', '=', 'contacts.doctor_id')
->where('contacts.type_id', '=', 1);
})
->join('provinces', 'contacts.province_id', '=', 'provinces.id')
->select('contacts.province_id as id','provinces.name as name',DB::raw("COUNT('contacts.province_id') as count"))
->groupBy('contacts.province_id');
if(!empty($IDs))
$query->whereIn('doctors.id', $IDs);
return $query->get();
}
Thanks for your help.
Currently, JoinClause doesn't support whereIn method, but you can overwrite it following way:
$query =
DB::Table('doctors')
->join('users', function($join){
$join->on('users.id', '=', 'doctors.user_id')
->where('users.status_id', '=', 3)
->orWhere('users.status_id', '=', 4)
->orWhere('users.status_id', '=', 5);
})
->join('contacts', function($join){
$join->on('doctors.id', '=', 'contacts.doctor_id')
->where('contacts.type_id', '=', 1);
})
->join('provinces', 'contacts.province_id', '=', 'provinces.id')
->select('contacts.province_id as id','provinces.name as name',DB::raw("COUNT('contacts.province_id') as count"))
->groupBy('contacts.province_id');