Ad
Laravel Sorting 1st Level Collection By 2nd Level Value
I have users with (or without) posts and I want the order of users to be dictated by the last post (showing the users who have never posted, then posted the longest ago first).
User A has posted yesterday
User B posted a week ago.
User C has never posted.
The order of users should be C,B,A.
The following code is how far I've come, but I can't seem to get it right. The order of users is not as I would expect it to be. Any thoughts?
UPDATE:
After removing the limit() on eager-load, The result of sorting is:
C,A,B, instead of C,B,A.
$users = User::with('roles', 'flags')->whereHas('subscription', function($x) use($category_id) {
$x->where('subscription.category_id', '=', $category_id);
})->get()->load([
'posts' => function($y) {
return $y->orderBy('created_at', 'ASC');
}
]);
$users = $users->sortBy(function($user) {
if(count($user->posts) == 0) {
return 0;
}
return strtotime($user->posts[0]->created_at);
});
return $users;
Ad
Answer
Thanks to @patricus in the comment section, my final (working) code is:
$users = User::with('roles', 'flags')->whereHas('subscription', function($x) use($category_id) {
$x->where('subscription.category_id', '=', $category_id);
})->get()->load([
'posts' => function($y) {
return $y->latest();
}
]);
$users = $users->sortBy(function($user) {
if(count($user->posts) == 0) {
return 0;
}
return strtotime($user->posts[0]->created_at);
});
return $users;
Ad
source: stackoverflow.com
Related Questions
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → Sorting Children in Laravel : How do I sort children by name?
- → React.js - Implementing sorting of components
- → How can I sort a javascript array while ignoring articles (A, an, the)?
- → How do I sort a whole element based off of one piece of it?
- → More performant way of sorting by selected first
- → Where should I handle sorting in Redux App?
- → Combining relationship sorting and filtering
- → Reorder lines in a string based on first two numbers inside
- → Sorting arrays with uncertain data
- → Overwrite the this value in array.prototype
- → jquery sorting table after ajax response
- → Woocommerce SEO -- Noindex 'Order by' archive
Ad