Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

neon5 neon5 Joined 21 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.