title: I am trying to mix pivot table values in Laravel
body: <p>I have three table category and products with its pivot table as category_product</p>
  
  <p>One category has many products</p>
  
  <p>I want the category name in attributes of products with its own field.</p>
  
  <pre><code>    $saijal = "Our Products";
      $products = Product::orderBy('created_at','DESC')-&gt;with('categories')-&gt;get();
  
      $abc = new Collection();
      foreach($products as $pro)
      {
          $abc-&gt;put($pro-&gt;id,$pro);
      }
  
      foreach($products as $k =&gt; $pro)
      {
          $abc-&gt;get($pro-&gt;id)-&gt;children = new Collection();
          $abc-&gt;get($pro-&gt;id)-&gt;categoryName= $pro-&gt;categories-&gt;name;
  
          $abc-&gt;get($pro-&gt;id)-&gt;children-&gt;push($pro);
          unset($pro[$k]);
  
      }
      dd($abc);
  </code></pre>
  
  <p>For example:</p>
  
  <p><a href="https://i.stack.imgur.com/kaYpX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kaYpX.png" alt="enter image description here"></a></p>