Lets say I have a Post object. I want the starred ones to subsequently filter them based on some conditions.
For some years I kept doing it this way:
Post::where('is_starred', true)->get()->filter(function($post){
...
});
This works great but today I found out that even if I remove the get()
call it still works. I don't know exactly why this works because where()
returns a QueryBuilder and filter()
is a collection method. The same happens if instead a filter()
I call some other collection methods like each()
.
Post::where('is_starred', true)->filter(function($post){
...
});
So my question is: Can collection methods be applied to QueryBuilder? Why? I looked at the QueryBuilder class to find out how these methods can be applied there but I didn't find anything. Is there something about that in Laravel's documentation?