Greetings, if you already has a results grabbed from the DB, you could use sortBy() on collection
delmadord said:
Greetings, if you already has a results grabbed from the DB, you could use sortBy() on collection
Thank you for the reply. I am still stumped on how to get the account of the favourites in that spot thought. So if I do a collection sort as follows:
$posts = $posts->sortBy(favourites->count);
How so I tell it to sort by that?
I would be interested in eloquent solution to this too. SortBy on Eloquent collection is possible, but is sorting on application level, which is not very efficient.
Only way I know about right now, is writing the query using fluent and do the sorting on database level.
Here ya go
Just add this to the query chain:
->selectRaw('*, (SELECT COUNT(*) FROM favourites WHERE favourites.post_id = posts.id) favourites_count')->
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community