title: trying to get property of non-object - Laravel?
body: <p>I'm doing a blog to learn how to use laravel. The blog is mostly finished but I still found some mistakes in my code. Most of them are solved but there is still one error I can't fix. </p>
  
  <p>It's about the 'my thread' of the user. If the user did a new account, he also get a 'my thread' page. If he's klicking on the page, he will get a view with all his threads. My problem now is, that if he don't have any threads, he will get a: </p>
  
  <p>trying to get property of non-object exception. Of course I don't want him to see this. </p>
  
  <p>So this is the function in my controller to his 'my thread' page:</p>
  
  <pre><code>public function startpage($id)
      {
  //        $userthreads = Thread::query()-&gt;where('user_id', $id)-&gt;get();
  //        return $userthreads;
          try {
              return view('test.startpage', [
                  'userthreads' =&gt; Thread::query()-&gt;where('user_id', $id)-&gt;get(),
                  'user' =&gt; User::query()-&gt;findOrFail($id)
              ]);
          } catch (ModelNotFoundException $e) {
              return view('test.notfound');
          }
      } 
  </code></pre>
  
  <p>with the first two ( uncommented ) lines, I checked if there was something in the array. I just got </p>
  
  <p><code>'[]'</code> </p>
  
  <p>as an output. That means the array is empty. I returned the $userthreads variable to the view and on my view I did this to avoid the problem: </p>
  
  <pre><code>            @if(empty($userthreads))
                  &lt;a href="{{ action('Test\\TestController@add') }}"&gt;Add Thread &lt;/a&gt;
              @else
              @foreach($userthreads as $uthread)
                      &lt;ul&gt;
                          &lt;a href="{{ action('Test\\TestController@show', [$uthread-&gt;id]) }}"&gt;{{$uthread-&gt;thread}}&lt;/a&gt;
                      &lt;/ul&gt;
              @endforeach
              @endif
  </code></pre>
  
  <p>This haven't worked for me and I can't see why. Maybe someone of you can help me there. </p>
  
  <p>Thanks for any help! </p>
  
  <p>Current code of Alexey Mezenin answers: </p>
  
  <pre><code>public function startpage($id)
      {
          try {
              $arr = [];
              $arr['userthreads'] = Thread::where('user_id', $id)-&gt;get();
              $arr['user'] = User::findOrFail($id);
  
              return view('test.startpage', $arr);
  
          } catch (ModelNotFoundException $e) {
              return view('test.notfound');
          }
      }
  </code></pre>
  
  <p>HTML: </p>
  
  <pre><code>@if(count($userthreads) &gt; 0)
              &lt;a href="{{ action('Test\\TestController@add') }}"&gt;Thread hinzufügen&lt;/a&gt;
              @else
              @foreach($userthreads as $uthread)
                      &lt;ul&gt;
                          &lt;a href="{{ action('Test\\TestController@show', [$uthread-&gt;id]) }}"&gt;{{$uthread-&gt;thread}}&lt;/a&gt;
                      &lt;/ul&gt;
              @endforeach
              @endif
  </code></pre>
  
  <p>Error :</p>
  
  <pre><code>Trying to get property of non-object (View: /var/www/laravel/logs/resources/views/test/startpage.blade.php)
  </code></pre>