title: Laravel get model from ID with belongtoMany
body: <p>I'm building an application using Laravel 4 but have some problems with the pivot tables.</p>
  
  <p>there are 3 tables Categories , Products , products_categories (pivot)</p>
  
  <p>Category model </p>
  
  <pre><code>public function product()
  {
      return $this-&gt;belongsToMany('Product', 'products_categories');
  
  }
  </code></pre>
  
  <p>Products Model </p>
  
  <pre><code>public function category()
  {
      return $this-&gt;belongsToMany('Category', 'products_categories');
  }
  </code></pre>
  
  <p><code>products_categories</code> table has <code>product_id</code> and <code>category_id</code> columns.</p>
  
  <p>What I want is take all products in this category and list them in views</p>
  
  <pre><code>$category = Category::where('id' , '=' , '7')-&gt;first();
  
      foreach($category-&gt;product as $product){
  
          echo $product-&gt;id;
      }
  </code></pre>
  
  <p>I can see product ids related with particular category but when I want to use it to get all product itself like:</p>
  
  <pre><code>    $category = Category::where('id' , '=' , '7')-&gt;first();
  
      foreach($category-&gt;product as $product){
  
          $product = Product::where('id' , '=' , $product-&gt;id )-&gt;get();
      }
  
      return View::make('index')-&gt;with('product',$product);
  </code></pre>
  
  <p>it doesn't work :( with this error </p>
  
  <blockquote>
    <p>Trying to get property of non-object </p>
  </blockquote>
  
  <p>I tried this </p>
  
  <pre><code>$category = Category::where('id' , '=' , '7')-&gt;first();
  
      $product = array();
  
      foreach($category-&gt;product as $product){
  
          $product[] = Product::where('id' , '=' , $product-&gt;id )-&gt;get();
  
      }
  
      return View::make('index')-&gt;with('product',$product);
  </code></pre>
  
  <p>this time it throws this error </p>
  
  <blockquote>
    <p>Missing argument 2 for Illuminate\Database\Eloquent\Model::setAttribute()</p>
  </blockquote>
  
  <p>How can I solve this?</p>