title: Laravel ErrorException Trying to get property of non-object
body: <p>I'm getting this error with this code</p>
  
  <pre><code>&lt;?php foreach(App\Campaign::all() as $campaign) { ?&gt;
      &lt;?php
          $category_id =  $campaign-&gt;category-&gt;id;
          $emailTemplates = App\EmailTemplate::where('category_id', $category_id);
          $lastEmailNumber = $emailTemplates-&gt;latest()-&gt;first()-&gt;number;
          $currentEmailNumber = $campaign-&gt;emails-&gt;last()-&gt;number;
      ?&gt;
  
      &lt;?php for ($i = $currentEmailNumber + 1; $i &lt; $lastEmailNumber; $i++) { ?&gt;
          &lt;?php $pass = $emailTemplates-&gt;where("number", $i)-&gt;first()-&gt;password; ?&gt;
      &lt;?php } ?&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>I know that for this time <code>$currentEmailNumber = 0</code> and <code>$lastEmailNumber = 14</code>
  The problem is coming from the variable <code>$i</code> on line 9. If instead of using $i, I use any number from 1 to 14 it does not get any errors. For example:</p>
  
  <pre><code>&lt;?php $pass = $emailTemplates-&gt;where("number", 10)-&gt;first()-&gt;password; ?&gt;
  </code></pre>
  
  <p>But if I display only <code>$i</code>, without all that sentence it displays correctly.
  Maybe it is cast problem.</p>