title: Trying to get property of non-object in Laravel Relationship
body: <p>I have two tables in my database, an Articles table and a Users table. An Article is posted by a user and a foreign key exists called 'created_by' referring to the 'user_id' field in the Users table. </p>
  
  <p>My Laravel models are as follows:</p>
  
  <p>User model</p>
  
  <pre><code>class User extends Model
  {
     use Notifiable;
  
     protected $primaryKey = 'user_id';
  
     public function articles()
     {
         return $this-&gt;hasMany('App\Article', 'created_by', 'user_id');
     }
  
  }
  </code></pre>
  
  <p>Article Model</p>
  
  <pre><code>class Article extends Model
  {
  
     protected $primaryKey = 'article_id';
  
     public function user()
     {
         return $this-&gt;belongsTo('App\User', 'created_by' 'user_id');
     }
  
  }
  </code></pre>
  
  <p>I have a view dashboard which displays all the articles and the name of the person who posted it and their image.</p>
  
  <p>Dashboard Controller</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\User;
  use App\Article;
  use App\Http\Controllers\Controller;
  use Illuminate\Http\Request;
  
  class DashboardController extends Controller
  {
      public function index (Request $request) 
      {
          if($request-&gt;session()-&gt;get('username')) {
  
              $articles = Article::all();
  
              return view('dashboard', compact('articles'));
  
          }
          else
              return redirect('login');
  
      }
  }
  </code></pre>
  
  <p>And finally the section of my view I'm getting the 'Trying to get property of non-object' error for.</p>
  
  <pre><code>@foreach($articles as $article)
          &lt;div class="row"&gt;
              &lt;div class="col-lg-1"&gt;&lt;/div&gt;
                  &lt;div class="col-lg-9"&gt;
                      &lt;div class="panel panel-info"&gt;
                          &lt;div class="panel-heading"&gt;
                              &lt;h3 class="panel-title"&gt;{{ $article-&gt;title }}                                    &lt;/h3&gt;
                          &lt;/div&gt;
                          &lt;div class="panel-body"&gt;
                              &lt;p&gt;
                              {{ $article-&gt;content }}
                              &lt;br&gt;&lt;br&gt; - {{ $article-&gt;user-&gt;first_name }}
                              &lt;/p&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;div class="col-lg-1"&gt;&lt;center&gt;&lt;br&gt;&lt;br&gt;
                  &lt;img src="{{ asset('images/people/$article-&gt;user-&gt;image') }}" class="img-circle" alt="Person"&gt;&lt;/center&gt;
              &lt;/div&gt;
          &lt;/div&gt;&lt;br&gt;&lt;br&gt;
      @endforeach
  </code></pre>
  
  <p>It seems to be the $article->user->first_name that I am having trouble with. If I type $article->user alone, the page will load but nothing appears in that area. </p>
  
  <p>Any ideas, I have been searching for solutions for the last two hours. </p>