I have a problem with orderBy in more complicated query.
I want to get 10 ordered users (pivot with club) from specified club and hide some columns.
So I have query in eloquent which almost works for me. The problem is with ordering users by some variable (level or exp - don't know yet).
My query (working - without ordering):
$users = Club::find($club_id)->users->each(function($row) {
$row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);
})->take(10);
return $users;
One of my trials (with OrderBy):
//
// $users = Club::find($club_id)->with(['users' => function ($q) {
//
// $q->orderBy('id');
//
// }])->each(function($row) {
//
// $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);
//
// });
//
// return $users;
Error:
The Response content must be a string or object implementing __toString(), "boolean" given.
I have appended attributes in my model. This attributes cannot be unselected by not including them in select, so they are basically always selected.
I want to create eloquent query which will limit attributes to only few, which interest me.
Ok, I figure it out in different way. Maybe it not looks so good but it works. Works much faster than without hidden attributes.
I use in my models appended attributes, which are always selected (even without selecting them in eloquent) so I had to create array with attributes which interest me.
$users = Club::where('id', $club_id)->first()->users()->orderBy('id')->limit(10)->get();
$ranking = [];
foreach($users as $k => $user){
$ranking[$k]['id'] = $user['id'];
$ranking[$k]['f_name'] = $user['f_name'];
...
}
return $ranking;
Thank you guys for clues.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments