title: Laravel 5 Pass data from two tables to the view
body: <p>I'm using Laravel to create a CRUD application but I'm a little stuck. What I want to do is to display the coresponding Category from 'categories' table for each Product from the 'products' table. I tried to squeeze the two tables in one view and create a @foreach within the @foreach but that only listed all the categories instead of assigning the coresponding category based on the category_id from the products table.</p>
  
  <p>Controller</p>
  
  <pre><code>$products = products::all();
  $categories = categories::all();
  
  return View::make('products.index')-&gt;with('products', $products)-&gt;with('categories', $categories);
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@foreach($products as $key =&gt; $value)
      &lt;tr&gt;
          &lt;td&gt;{!! $value-&gt;id !!}&lt;/td&gt;
          &lt;td&gt;{!! $value-&gt;product_name !!}&lt;/td&gt;
          @foreach($categories as $key2 =&gt; $value2)
              &lt;td&gt;{!! $value2-&gt;category !!}&lt;/td&gt;
          @endforeach
          &lt;td&gt;{!! $value-&gt;product_price !!}&lt;/td&gt;
          &lt;td&gt;{!! $value-&gt;updated_at !!}&lt;/td&gt;
          &lt;td&gt;
  @endforeach
  </code></pre>
  
  <p>products table</p>
  
  <pre><code>    Schema::create('products', function($table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('category_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;string('product_name', 255)-&gt;unique();
          $table-&gt;decimal('product_price', 10, 4);
          $table-&gt;dateTime('updated_at')-&gt;default(DB::raw('CURRENT_TIMESTAMP'));
      });
  
      Schema::table('products', function($table) {
          $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories');
      });
  </code></pre>
  
  <p>categories table</p>
  
  <pre><code>    Schema::create('categories', function($table)
      {
          $table-&gt;increments('id');
          $table-&gt;string('category', 255)-&gt;unique();
          $table-&gt;dateTime('updated_at')-&gt;default(DB::raw('CURRENT_TIMESTAMP'));
      });
  </code></pre>