Sok Mengheang Sok Mengheang - 1 year ago 105
PHP Question

laravel 5.2 many to many relation retrieve data with intermediate table

I have tables :


  • id


  • id


  • product_id

  • category_id

by using eloquent model relation, how to get all product data from a specific category id?

Answer Source

Assuming that you defined relationships on your models, you can try this:

$productName = 'Some product';

$category = Category::findOrFail($categoryId);
$product = $category->products->where('product_name', '=', $productName)->get();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download