title: best approach to search from pivot table using laravel
body: <p>Searching from  pivot table using laravel.</p>
  
  <p>Here is my table structure:</p>
  
  <pre><code>Product
  id
  name
  
  Categories
  id
  name
  
  product_category (Pivot table)
  
  id
  category_id
  product_id
  
  //products can have multiple categories 
  </code></pre>
  
  <p>Product model:</p>
  
  <pre><code>public function categories(){
       return $this-&gt;belongsToMany(Category::class, 'product_category');
  }
  </code></pre>
  
  <p>What is the best way to search all products by category id?
  Currently I am doing this way, and it seems not an efficient way:</p>
  
  <pre><code>//Controller
  $categories = product_category::where('category_id',1)-&gt;get();
  </code></pre>
  
  <p>Now I have to loop through categories and then get Products and pass it to views? Any idea how to do this in an efficient way?</p>