Q:

Why am I getting next error: Trying to get property of non-object?  laravel

class Article extends Model
{       
    protected $fillable = ['category_id','author_id','title','img','description','text'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }    
    public function article_category()
    {
        return $this->belongsTo('App\ArticleCategory');
    }
}

class ArticleCategory extends Model
{    
    protected $table = 'article_categories';    
    protected $fillable = ['name','hide'];    
    public function article()
    {
        return $this->hasMany('App\Article');
    }
}

User model
    public function article()
        {
            return $this->hasMany('App\Article');
        }

Why am I getting error when trying to get author name and article category name? 
Trying to get it in view this way:
$article->user->name
$article->article_category->name
Can't understand the reason :c

A:

Try to change the 
return $this->belongsTo('App\User');

and 
return $this->belongsTo('App\ArticleCategory');

To 
return $this->belongsTo('App\User', 'foreign_key');
and 
return $this->belongsTo('App\ArticleCategory', 'foreign_key);

Both will receive the properly foreign key that you provide in yours tables on migrations.  Perhaps your foreign keys doesn't recognized. 