title: firstOrFail returning the wrong record from Eloquent Query Builder
body: <p>I have a table in my databases called <code>tallies</code> that keeps track of the <code>count</code> for particular entities. The two key fields in the table are:</p>
  
  <ul>
  <li><strong>type</strong>: the name of the entity</li>
  <li><strong>count</strong>: the count of the entity</li>
  </ul>
  
  <p>Right now in the table I have two records, the <strong>first</strong> record has a type of <code>hardDrives</code> and the <strong>second</strong> has a type of <code>monitors</code>.</p>
  
  <p>I have a method in my repository used for incrementing the count for a particular record in a <code>tallies</code> table:</p>
  
  <pre><code>public function decreaseCountBy($type, $number){
  
      $countCollection = $this-&gt;tally-&gt;where('type', '=', $type )-&gt;firstOrFail();
  
      $record = $countCollection-&gt;first();
  
      // Troubleshooting output
      die(var_dump([$type, $record-&gt;toArray()]));
  
      // The rest of the method
      $record-&gt;count -= $number;
      $result = $record-&gt;save();
      if(!$result){
          throw new \Exception('Error saving decrementation');
      }
      return $record-&gt;count;
  }
  </code></pre>
  
  <p>When I send the request to increment <code>monitors</code> and look at the output of the troubleshooting die and dump in this method and get the following output: </p>
  
  <pre><code>array (size=2)
    0 =&gt; string 'monitors' (length=8)
    1 =&gt; 
      array (size=5)
        'id' =&gt; int 4
        'type' =&gt; string 'hardDrives' (length=10)
        'count' =&gt; int 15
        'created_at' =&gt; string '2014-12-21 03:50:04' (length=19)
        'updated_at' =&gt; string '2014-12-21 14:35:28' (length=19)
  </code></pre>
  
  <p>Even though I'm using <code>monitors</code> as the value for <code>$type</code> in the query, I get the record for <code>hardDrives</code>.</p>
  
  <p>After this, I tried changing the method to fire the query:</p>
  
  <pre><code>$countCollection = $this-&gt;tally-&gt;where('type', $type )-&gt;get();
  </code></pre>
  
  <p>And then I get the correct result:</p>
  
  <pre><code>array (size=2)
    0 =&gt; string 'monitors' (length=8)
    1 =&gt; 
      array (size=5)
        'id' =&gt; int 5
        'type' =&gt; string 'monitors' (length=8)
        'count' =&gt; int 3
        'created_at' =&gt; string '2014-12-21 03:50:04' (length=19)
        'updated_at' =&gt; string '2014-12-21 03:50:04' (length=19)
  </code></pre>
  
  <p>I could stop here and add in my own exception throw if there is an error finding the record, but when I read the <a href="http://laravel.com/api/4.2/" rel="nofollow">API Documentation</a> for the Builder class' method <code>firstOrFail()</code> (sorry I can't link directly to it), the method is described as:</p>
  
  <blockquote>
    <p>Execute the query and get the first result or throw an exception.</p>
  </blockquote>
  
  <p>I'd like to use the built in Laravel Exception that get's thrown when the records is not found rather than using my own. </p>
  
  <p>Is there something I'm missing here? When I look for other examples in the laravel <a href="http://laravel.com/docs/4.2/eloquent" rel="nofollow">Eloquent</a> documentation it looks like I'm building the query correctly. </p>
  
  <p>More than anything else I'd like to know <em>why</em> it's failing rather than just a work around. </p>
  
  <p><strong>Resolution</strong></p>
  
  <p>Here's the final version of the method just to show everyone how it ended up:</p>
  
  <pre><code>public function decreaseCountBy($type, $number){
      $record = $this-&gt;tally-&gt;where('type', '=', $type )-&gt;firstOrFail();
      $record-&gt;count -= $number;
      $result = $record-&gt;save();
      if(!$result){
          throw new \Exception('Error saving decrementation');
      }
      return $record-&gt;count;
  }
  </code></pre>
  
  <p>Normally when you perform a <code>-&gt;get();</code> to retrieve the data, the result is an eloquent <code>Collection</code> instance holding multiple records. From there if you wanted to retrieve only the first record you could use the <code>-&gt;first()</code> method of the Collection class to get an eloquent <code>Model</code> class instance with that record's information. </p>
  
  <p>In the case of <code>firstOrFail()</code>, what you're telling the query builder is that you only want the first record if found. Because you will only ever receive one record's worth of data, eloquent skips the collection and returns a model instance. </p>
  
  <p>In my code above I've removed the line that "grabs the model of the first record", i.e. <code>$record = $countCollection-&gt;first();</code>, and renamed the variables to fit the expected results better, i.e. <code>$record</code> in place of <code>$countCollection</code>. </p>