You can use whereHas for this:
$category->whereHas('events', function($query) {
$query->where('approved', Null);
})->get();
Docs: https://laravel.com/docs/5.8/eloquent-relationships#querying-relationship-existence
Hi All,
I am trying to understand how to filter my eager loaded results. In my show method on my category controller I have
public function show(Category $category)
{
$category->load('events');
return view('category.show', compact('category'));
}
This returns my current category with the eager loaded events so my relationships are working. My question is how do I filter those returned events. I guess I should use a where() method but when I use
$category->load('events')->where('approved', Null)->get();
It is searching my categories class for the approved column, not my events class. Is there a way to tell Laravel to search the eager loaded class?
Thanks!
$category->load(['events' => function ($query) {
$query->whereNotNull('approved');
}])->get();
remove $category=
Please or to participate in this conversation.