title: Passing data from multiple tables to a laravel view in the right way
body: <p>I'm new to Laravel and this is the first framework I use, I'm loving it so far. I have a question about how to pass data to a view from multiple tables. </p>
  
  <p>I'm working in a fashion application who has an outfit model. The outfit can belong to one or more brands, one or more fashion styles, and one or more stores. I defined the relationships using Laravel, and I came up to the following model: </p>
  
  <p>Outfit Model:</p>
  
  <pre><code>protected $table = 'outfits';
  
  [...]
  
  public function brands(){
      return $this-&gt;belongsToMany('Brand','outfit_brands','outfit_id','brand_id');
  }    
  
  public function styles(){
      return $this-&gt;belongsToMany('Style','outfit_styles','outfit_id','style_id');
  }
  
  public function stores(){
      return $this-&gt;belongsToMany('Store','outfit_stores','outfit_id','store_id');
  }
  
  [...]
  </code></pre>
  
  <p>Now I can go to my Outfit controller and to pass data into the view like this :</p>
  
  <pre><code>$outfits = Outfit::all();
  
  foreach($outfits as $item){
  
      $id = $item-&gt;id;
  
      $item-&gt;mybrands = Outfit::find($id)-&gt;brands-&gt;lists('name');            
      $item-&gt;mystyles = Outfit::find($id)-&gt;styles-&gt;lists('name');
      $item-&gt;mystores = Outfit::find($id)-&gt;stores-&gt;lists('name');
  }        
  return View::make('outfits.index', compact('outfits'));
  </code></pre>
  
  <p>In the view, I get all the data by doing this :</p>
  
  <pre><code>[...]
  
  @foreach ($outfits as $item)
  
  [...]
  &lt;td&gt;{{ $item-&gt;name }}&lt;/td&gt;                      
  &lt;td&gt;{{ $item-&gt;description }}&lt;/td&gt;   
  
  [...]
  
  &lt;td&gt;{{ HTML::ul($item-&gt;mybrands, array('class'=&gt;'list-unstyled')); }}&lt;/td&gt;
  &lt;td&gt;{{ HTML::ul($item-&gt;mystyles, array('class'=&gt;'list-unstyled')); }}&lt;/td&gt;
  &lt;td&gt;{{ HTML::ul($item-&gt;mystores, array('class'=&gt;'list-unstyled')); }}&lt;/td&gt; 
  
  [...]
  
  @endforeach          
  </code></pre>
  
  <p>This works perfectly, but I'm not sure if this is the proper or the optimal way to pass data to a view. I want to do things well from the start, so I'm here to check with you guys about what is the right way to achieve the same result properly.</p>
  
  <p>PS: I read online documentation about view composers, which seems to be a fair alternative, but it was quite complex for my level of knowledge and it assumes a lot of things that I don't know how to do yet. </p>
  
  <p>Thank you in advance guys, I appreciate your help.</p>