title: Show data to view with one to many relationship in Laravel
body: <p>I have 2 tables:</p>
  
  <pre><code>Type: id, name
  Question: id, content, type_id
  </code></pre>
  
  <p>I've show data with filter function but when I'm trying to show type name and level name, I got this error</p>
  
  <blockquote>
    <p>Trying to get property 'name' of non-object</p>
  </blockquote>
  
  <p>When I dump($data), i got this:</p>
  
  <pre><code>LengthAwarePaginator {#217 ▼
    #total: 6
    #items: Collection {#229 ▼
      #items: array:6 [▼
        0 =&gt; Question {#230 ▶}
        1 =&gt; Question {#231 ▶}
        2 =&gt; Question {#232 ▶}
        3 =&gt; Question {#233 ▶}
        4 =&gt; Question {#234 ▶}
        5 =&gt; Question {#235 ▶}
      ]
    }
    #path: "http://127.0.0.1:8000/test"
    #query: array:1 [▶]
    ...
  }
  </code></pre>
  
  <p>This is my Model
  Question.php</p>
  
  <pre><code>protected $table = 'question';
      public $primaryKey = 'id';
      public $timestamps = false;
  
      public function types()
      {
          return $this-&gt;hasMany(Type::class,'type_id','id');
      }
  </code></pre>
  
  <p>Type.php</p>
  
  <pre><code>protected $table = 'type';
      public $primaryKey = 'id';
      public $timestamps = false;
      protected $fillable = ['type_id'];
  
      public function questions(){
          return $this-&gt;belongsTo(Question::class);
      }
  </code></pre>
  
  <p>TestController.php</p>
  
  <pre><code>public function getTest(Request $request){
          $types = Type::all();
          $model = Question::where('id', '&gt;', 0);
  
          if (isset($request-&gt;type))
              $model = $model-&gt;where('type_id', $request-&gt;type);
  
          $data = $model-&gt;paginate(15)-&gt;appends(request()-&gt;query());
  
          return view('filter', compact( 'data', 'request', 'types'));
      }
  </code></pre>
  
  <p>And this is my view</p>
  
  <pre><code>&lt;form method="get"&gt;
      &lt;select name="type" onchange="this.form.submit()"&gt;
          &lt;option value=""&gt;All types&lt;/option&gt;
          @foreach ($types as $item)
              &lt;option value="{{ $item-&gt;id }}" @if ($request-&gt;type == $item-&gt;id) selected @endif&gt;{{ $item-&gt;name }}&lt;/option&gt;
          @endforeach
      &lt;/select&gt;
  &lt;/form&gt;
  &lt;table border="1"&gt;
      &lt;tr&gt;
          &lt;th&gt;ID&lt;/th&gt;
          &lt;th&gt;Content&lt;/th&gt;
          &lt;th&gt;Type&lt;/th&gt;
      &lt;/tr&gt;
      @foreach ($data as $q)
          &lt;tr&gt;
              &lt;td&gt;{{ $q-&gt;id }}&lt;/td&gt;
              &lt;td&gt;{{ $q-&gt;content }}&lt;/td&gt;
              &lt;td&gt;{{ $q-&gt;type_id-&gt;name }}&lt;/td&gt;
          &lt;/tr&gt;
      @endforeach
  &lt;/table&gt;
  </code></pre>