get()
must be run last. Remove it from the first line and add it to the second and third.
$allcars = $user->cars()->bySortOrder();
$activecars = $allcars->where('shelved','<>','1')->get();
$shelvedcars = $allcars->where('shelved','1')->get();
If you need to get()
$allcars
you can do this:
$query = $user->cars()->bySortOrder();
$allcars = $query->get();
$activecars = $query->where('shelved','<>','1')->get();
$shelvedcars = $query->where('shelved','1')->get();
If you want to filter the hits after a query has been done you need to foreach
all the hits and use if
to sort out what you want and add it to a new array
.
Hi Marwelln,
Yes, I do need $allcars too. So in the second example does that do one query to the database or is it the get() that does three queries to the database?
I'm thinking that you are saying it's doing three queries and if I want only one query done then I need to get everything and foreach into separate collections. Is that right?
Thank you.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community