Hey folks,
I'm stuck with the following. I am building a query based on a filter from a form.
One dimension that I need to filter for, is a distant relationship.
Here's the concrete example:
I'm having posts and cuisine, every posts has one or multiple cuisines (and thus also vice versa). cuisine and post are connected using a pivot table cuisine_post.
in my cuisine.php:
public function posts()
{
return $this->belongsToMany(post::class);
}
In my post.php:
public function cuisines()
{
return $this->belongsToMany(cuisine::class);
}
Thus, I can successfully access post->cuisines and vice versa.
Now in my query, I need to return a collection of post objects that have certain cuisines. Cuisines are selected using checkboxes on the filter, return values are the respective cuisine_id.
How do I now filter my posts to certain cuisines?
Btw, here's the rest of the filter query (which works fine), with cuisine still missing.
$posts = post
::when($request->get('pricerange'), function($query) use($request) {
$query->whereIn('pricerange',$request->get('pricerange'));
})
->when($request->get('is_vegan'), function($query) {
$query->where('is_vegan',true);
})
->when($request->get('is_date'), function($query) {
$query->where('is_date',true);
})
->when($request->get('is_delivery'), function($query) {
$query->whereNotNull('url_delivery');
})
->when($request->get('is_menu'), function($query) {
$query->whereNotNull('url_menu');
})
->paginate(5);
Thanks for your help!