DEV Community

Discussion on: Laravel 8.x withExists Method to Eloquent Queries Example

Collapse
 
bdelespierre profile image
Benjamin Delespierre

That's amazing, I didn't know about this feature. One question tho, how is it different from

User::has('posts')->get();
Enter fullscreen mode Exit fullscreen mode

?

Collapse
 
skeemer profile image
Leo Lutz

It actually does a JOIN. So it can cut down on queries run.

Collapse
 
sureshramani profile image
Suresh Ramani

has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.