title: Laravel Query Builder - How to get only the last records matching query
body: <p>Using Laravel 5.1 I have a stocks table with ticker and company name in a one-to-many relationship with a price table containing quote prices updated every 15 minutes. Can anyone enlighten me on the best way to get just the latest quote returned from the join of the two tables? Maybe another solution that I have not thought of? Here is my join that returns this for every stock and price in the database.</p>
  
  <pre><code>$quotes = DB::table('stocks')
      -&gt;join('stock_prices', 'stocks.id', '=', 'stock_prices.stock_id')
      -&gt;get();
  return $quotes;    
  </code></pre>
  
  <p>Which returns</p>
  
  <pre><code>[
  {
  "id": 25,
  "symbol": "MSFT",
  "last_price": "53.60",
  },
  {
  "id": 25,
  "symbol": "MSFT",
  "last_price": "54.80",
  },
  {
  "id": 30,
  "symbol": "BA",
  "last_price": "144.74",
  },
  {
  "id": 30,
  "symbol": "BA",
  "last_price": "145.60",
  },
  ] 
  </code></pre>
  
  <p>Migrations</p>
  
  <pre><code>public function up()
  {
      Schema::create('stock_prices', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;decimal('last_price', 6, 2);
          $table-&gt;decimal('net_change', 6, 2);
          $table-&gt;decimal('percent_change', 4, 2);
          $table-&gt;decimal('open', 6, 2);
          $table-&gt;decimal('high', 6, 2);
          $table-&gt;decimal('low', 6, 2);
          $table-&gt;decimal('close', 6, 2);
          $table-&gt;integer('volume');
          $table-&gt;integer('stock_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;timestamps();
  
          $table  -&gt;foreign('stock_id')
                  -&gt;references('id')
                  -&gt;on('stocks')
                  -&gt;onDelete('cascade');
      });
  }    
  
  public function up()
  {
      Schema::create('stocks', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('symbol');
          $table-&gt;string('name');
          $table-&gt;boolean('confirmed');
          $table-&gt;timestamps();
      });    
  </code></pre>
  
  <p>So what would be the best way to get it to just return the last updated price for every stock? Either with working with the returned array or a new Laravel query builder? Or an Eloquent solution?</p>
  
  <p>I have spent 2 days searching and banging my head against the wall so any help will be GREATLY appreciated.</p>
  
  <p>Side notes: I plan to wipe out everything older than 30 days so the table will not be extremely large. Also while hacking through the array with some of the Laravel helpers I some times get an error saying it is not an array but an object and some times will say it is not an object but an array. Bonus points for explaining that to me.</p>