title: how to eager load in a Many to Many relationship in laravel 5?
body: <p>My <code>user table</code> and <code>item table</code> are related by a many to many relationship, the <code>item table</code> has some data in foreign tables, example <code>color table</code> I have a pivot table that relates the two.
  My question is, if I want to retrieve all the items associated to a user with a color how do I get them with eager loading?</p>
  
  <p>I know that <code>$user-&gt;item</code> will retrieve all the items for that user.</p>
  
  <p>But if I want to eager load all the items for that user with the color attributes in one query, how do I do it?
  At the moment I am looping through the items associated to the user and lazy loading every data I need, for example</p>
  
  <pre><code>foreach($user-&gt;item as $i){
  
        echo($i-&gt;item-&gt;color)
  
  }
  </code></pre>
  
  <p>This means every cycle makes a new query...</p>
  
  <p>here the models:</p>
  
  <p>User model:</p>
  
  <pre><code>public function item(){
          return $this-&gt;belongsToMany('App\item')-&gt;withTimestamps();
      }
  </code></pre>
  
  <p>item model:</p>
  
  <pre><code>public function user(){
          return $this-&gt;belongsToMany('App\User');
      }
  </code></pre>
  
  <p>and this the schema for the pivot table</p>
  
  <pre><code>Schema::create('item_user', function(Blueprint $table) {
  
              $table-&gt;unsignedInteger('user_id')-&gt;unsigned()-&gt;index();
              $table-&gt;foreign('user_id')
                  -&gt;references('id')
                  -&gt;on('users')
                  -&gt;onDelete('cascade');
  
              $table-&gt;unsignedInteger('item_id')-&gt;unsigned()-&gt;index();
              $table-&gt;foreign('item_id')
                  -&gt;references('id')
                  -&gt;on('items')
                  -&gt;onDelete('cascade');
  
              $table-&gt;timestamps();
          });
  </code></pre>