I have a category class and I am calling all the events inside that class with eager loading. However I only want to show the events that have been approved. I am using a show controller in Laravel.
I have been able to filter the category class using wherehas() but I can't figure out how to filter the result of the eager loaded events class.
public function show(Category $category)
{
$cat = $category->with('events')->get();
return $cat;
return view('category.show', compact('cat'));
}
Like I said, I've been able to filter the category but how do I send through the category with the events that have approved = 1 instead of null?
You can pass a callback to the with function that filters the query.
$cat = $category->with(['events' => function ($q) {
$q->where('approved', 1);
}]
)->get();