title: Laravel - foreach loop returns Trying to get property of non-object
body: <p>I have a collection of arrays (18), that ALL have a relation - <code>question</code>.</p>
  
  <p><strong>Function getting the data:</strong></p>
  
  <pre><code> $auditResults = Audit::where('audit_id', $id)
       -&gt;with('question', 'question.auditQuestion')
       -&gt;get();
  </code></pre>
  
  <p>Within the question array, I can see the attributes I'd like to access. When I attempt to access it, I get a <code>Trying to get property of non-object</code></p>
  
  <p><strong>Collection:</strong>   </p>
  
  <pre><code>Collection {#407 ▼
    #items: array:18 [▼
      0 =&gt; Audit {#411 ▶}
      1 =&gt; Audit {#412 ▶}
      2 =&gt; Audit {#413 ▶}
      ... shortened for brevity's sake
      14 =&gt; Audit {#425 ▶}
      15 =&gt; Audit {#426 ▶}
      16 =&gt; Audit {#427 ▶}
      17 =&gt; Audit {#428 ▶}
    ]
  }
  </code></pre>
  
  <p><strong>Output of an<code>Audit</code>:</strong></p>
  
  <pre><code>@foreach($auditResults as $answer)
      {{$answer}}
  @endforeach
  </code></pre>
  
  <hr>
  
  <pre><code>{  
         "id":1,
         "audit_id":1,
         "audit_questions_details_id":2,
         "question":{  
            "id":2,
            "audit_question_id":2,
            "question_number":1,
            "comment":1,
            "header":0,
            "created_at":"2017-03-27 12:16:50",
            "updated_at":"2017-03-27 12:16:50",
            "audit_question":{  
               "id":2,
               "audit_detail_id":1,
               "question":"Lorum Ipsum Dorum",
               "created_at":"2017-03-27 12:16:50",
               "updated_at":"2017-03-27 12:16:50"
            }
         },
         "score":null,
         "comment":null,
         "created_at":null,
         "updated_at":null
      }
  </code></pre>
  
  <p>If I then proceed to access the question relationship:</p>
  
  <pre><code>@foreach($auditResults as $answer)
      {{$answer-&gt;question}}
  @endforeach
  </code></pre>
  
  <p>It outputs only a number (I'm unsure what it actually is!, as I'd expect it to output the question relationship):</p>
  
  <pre><code>1
  2
  3
  4
  </code></pre>
  
  <p>Why is this only outputting a number, when it should be outputting the question array itself, as shown above?</p>
  
  <p>The error is shown when I try to access the <code>question</code> relation:</p>
  
  <pre><code>@foreach($auditResults as $answer)
      {{$answer-&gt;question-&gt;id}}
  @endforeach
  </code></pre>
  
  <p><strong>Audit Model:</strong></p>
  
  <pre><code>class Audit extends Model
  {
      protected $fillable = [
          'audit_id', 'audit_questions_details_id', 'question', 'score', 'comment'
      ];
  
      public function auditScore()
      {
          return $this-&gt;belongsTo('App\AuditScore', 'id', 'audit_id');
      }
  
      public function question(){
          return $this-&gt;belongsTo(AuditQuestionDetail::class, 'audit_questions_details_id');
      }
  
  }
  </code></pre>
  
  <p>Data of Audit Model:</p>
  
  <p><a href="https://i.stack.imgur.com/0cdrJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0cdrJ.png" alt="AuditModelData"></a></p>
  
  <p><strong>AuditQuestionDetail Model:</strong></p>
  
  <pre><code>class AuditQuestionDetail extends Model
  {
      protected $table = 'audit_questions_details';
  
      protected $fillable = [
          'audit_question', 'question_number', 'comment',
      ];
  
      public function auditQuestion()
      {
          return $this-&gt;hasOne(AuditQuestion::class, 'id', 'audit_question_id');
      }
  
      public function score()
      {
          return $this-&gt;hasMany(Audit::class);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/eicHh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eicHh.png" alt="AuditQuestionDetails Data"></a></p>
  
  <p>Many thanks.</p>