title: Laravel know variable inside function, but does not know in return
body: <p>I am using laravel excel to export my data. Inside map function, the $credit and $debit variables but when returning them, it throws a error.</p>
  <p>The error is :</p>
  <blockquote>
  <p>Trying to get property 'name' of non-object</p>
  </blockquote>
  <p>here is my map function:</p>
  <pre><code>public function map($partner): array
  {
      $credit = Bank::whereId($partner-&gt;credit)-&gt;first(); // also used: Bank::find($partner-&gt;credit);
      $debit = Bank::whereId($partner-&gt;debit)-&gt;first();
      dd($credit-&gt;name) // returns 'test'
  
      return [
          $partner-&gt;full_name, // this returns correctly.
          $credit-&gt;name, // returns &quot;Trying to get property 'name' of non-object&quot;
                         // if use $credit, it returns whole credit object.
          $debit-&gt;name,  // returns &quot;Trying to get property 'name' of non-object&quot;
                         // if use $credit, it returns whole debit object.
      ];
  }
  </code></pre>