My Eloquent models are described below:
class Product extends Eloquent{
...
public function categories(){
return $this->belongsToMany('Category');
}
...
}
class Category extends Eloquent{
...
public function products(){
return $this->belongsToMany('Product');
}
...
}
How do I write a cleanly structured query for whether a given product is in a category?
Extra love if it can be a method of the class!
My closest (and very messy) attempt is something like:
$categories = Category::all();
$product = Product::find($id);
$id = 3;
foreach($categories as $cat)
while($cat->products())
if($product->id === $cat->products()->id)
true
endif
endwhile
endforeach