title: Laravel Eloquent check on previous eager load relation
body: <p>I have some trouble with chaining relationships. I want to chain three of them, but this is not working properly: </p>
  
  <pre><code>return UserModel::with('cars.pieces.attributes')
  </code></pre>
  
  <p>I must check not only on pieces for the attributes but on the result given by cars.pieces. I must know both pieces and cars to give the good attributes</p>
  
  <p>I want to retrieve a user with the cars he chose then the pieces of the car and the attributes of these pieces. All of those things are chosen by the user.</p>
  
  <p>With only <code>cars.pieces.</code> I have my user, then the array of cars then the array of pieces for this car. When I add attributes, I have attributes not for pieces of cars of users but attributes for pieces whatever cars it is.</p>
  
  <p>It seems like the relationship is only looking for the previous relation and not the whole packet.</p>
  
  <p>UserModel.php</p>
  
  <pre><code>public function cars(){
      return $this-&gt;belongsToMany(CarsModel::class, 'user_cars', 'id_user','id_cars');
  }
  </code></pre>
  
  <p>Then in CarsModel.php</p>
  
  <pre><code>public function pieces(){
      return $this-&gt;belongsToMany(PiecesModel::class, 'cars_pieces', 'id_cars','id_pieces')
  }
  </code></pre>
  
  <p>And finally in PiecesModel.php : </p>
  
  <pre><code>public function attributes(){
      return $this-&gt;belongsToMany(AttributeModel::class, 'user_cars_pieces_attributes', 'id_attribute', 'id_piece')
  }
  </code></pre>
  
  <p>The last entity is using 4 fields for the primary key : </p>
  
  <pre><code>id_user, id_car, id_attribute, id_piece
  </code></pre>
  
  <p>My relation is probably something like this :</p>
  
  <p><a href="https://i.stack.imgur.com/mGe7R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mGe7R.png" alt="enter image description here"></a></p>
  
  <p>What could be a way to retrieve attributes for pieces of cars for the user?</p>
  
  <p>Thank you for helping!</p>
  
  <p>EDIT : I have an idea to do that. I can just retrieve pieces of cars and then for each pieces I check for their attributes based on where clause with both id_pieces and id_cars then combine to my original array but it would be not efficient imo</p>