title: Trying to get property of non-object Laravel
body: <p>So, I've created a controller to add topic in my app, and it stores it in database. The only problem is when I show a view it's giving me ErrorException.</p>
  
  <p>Here is the Topic Controller</p>
  
  <pre><code> public function store(Request $request)
  {
      $users = User::all();
  
      $userid = Auth::user()-&gt;id;
  
      $topic = new Topic([
        'user_id' =&gt; $userid,
        'title' =&gt; $request-&gt;get('title'),
        'description' =&gt; $request-&gt;get('description'),
        'tags' =&gt; $request-&gt;get('tags')
      ]);
  
      $topic-&gt;save();
  
  
      //Creating a loop for each user, send a notification.
       $message_content = "The " . Auth::user()-&gt;first_name . " " . Auth::user()-&gt;last_name . " just added a new topic. \n " . $topic-&gt;title . " \n ";
  
          foreach ( $users as $user ) {
  
              mail( $user-&gt;email, 'New Topic!', $message_content);
          }
  
      return redirect('/topics')-&gt;with('topics', $topics);
  
  
  
  }
  </code></pre>
  
  <p>Here is the view</p>
  
  <pre><code>@foreach($topics as $topic)
      &lt;div class="col-lg-4 col-md-6"&gt;
        &lt;div class="widget widget-shadow"&gt;
          &lt;div class="widget-content padding-20 bg-green-500 white height-full"&gt;
  
            &lt;a class="avatar pull-left margin-right-20" href="javascript:void(0)"&gt;
              &lt;img src=" {{URL::to('uploads')}}/{{ $topic-&gt;creator-&gt;image }} " alt=" "&gt;
            &lt;/a&gt;
            &lt;div style="overflow:hidden;"&gt;
              &lt;small class="pull-right grey-200"&gt;{{$topic['created_at']}}&lt;/small&gt;
              &lt;div class="font-size-18"&gt; {{$topic-&gt;creator-&gt;first_name}}  {{$topic-&gt;creator-&gt;last_name}}&lt;/div&gt;
              &lt;div class="grey-200 font-size-14 margin-bottom-10"&gt;{{$topic-&gt;creator-&gt;role}}&lt;/div&gt;
              &lt;blockquote class="cover-quote font-size-16 white"&gt;{{$topic['title']}}
              &lt;/blockquote&gt;
            &lt;/div&gt;
  
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      @endforeach
  </code></pre>
  
  <p>Here is the topic model</p>
  
  <pre><code>class Topic extends Model
  
  public $fillable = ['title', 'user_id', 'description', 'tags'];
  
  public function creator() {
      return $this-&gt;belongsTo( User::class, 'user_id', 'id');
  </code></pre>
  
  <p>and here is the user model(only relation to topic model)</p>
  
  <pre><code>public function hasTopic() {
      return $this-&gt;hasMany( Topic::class, 'id', 'user_id' );
  }
  </code></pre>
  
  <p>lastly here is the controller that gets the topic data and shows a view</p>
  
  <pre><code>public function sdp()
  {
  
      $topics = Topic::all();
  
      $users = User::with([ 'hasTopic' =&gt; function ($query) {
          $query-&gt;where('id', 'user_id');
  
      }]);
  
  
      return view('topics.student', ['topics' =&gt; $topics, 'users' =&gt; $users] );
  
  }
  </code></pre>