title: laravel 5.7 correct way of getting results on many to many relation
body: <p>I'm learning laravel framework and I've a pretty straightforward question:</p>
  
  <p>I have three tables:</p>
  
  <p>Foods table:</p>
  
  <pre><code>foods
  --------------
  id | name
  </code></pre>
  
  <p>Ingredients table:</p>
  
  <pre><code>ingredients
  ---------------
  id, | name
  </code></pre>
  
  <p>Pivot table:</p>
  
  <pre><code>food_ingredient
  ---------------
  food_id | ingredient_id
  </code></pre>
  
  <p>In the models I have the "belongsToMany" methods:</p>
  
  <p>Food Model:</p>
  
  <pre><code>public function ingredients(){
      return $this-&gt;belongsToMany('App\Models\Ingredient');
  }
  </code></pre>
  
  <p>Ingredient Model:</p>
  
  <pre><code>public function foods(){
      return $this-&gt;belongsToMany('App\Models\Food');
  }
  </code></pre>
  
  <p>FoodsController (index):</p>
  
  <pre><code>    $foods = Food::all();
  
      foreach($foods as $food){
          $food-&gt;ingredients = Ingredient::join('food_ingredient','food_ingredient.id','=','ingredients.id')
                                         -&gt;where('food_ingredient.food_id',$food-&gt;id)-&gt;get();
      }
  </code></pre>
  
  <p>I want to get the ingredients information (just the name until now) of each food, and this query (the one is in the foreach loop) does it, but I'm not sure if I'm doing it in a right way. I'm not using the belongsToMany relations so...there must be something wrong in my understanding.</p>
  
  <p>How could I get the names of the ingredients?</p>
  
  <p>Until know just those names are ok, but I'll add a third table "allergens" and it's pivot table "allergen_ingredient" soon. Then, I'll have to get each food, their ingredients, and also the allergens of each ingredient. So that, I want to do it well from now.</p>
  
  <p>Thank you in advance.</p>