试图获得非对象的属性 - Laravel 5
我试图在我的文章中回显用户的名称,我得到了
错误异常:尝试获取非对象的属性
我的代码:
模型
1. News
class News extends Model
{
public function postedBy()
{
return $this->belongsTo('App\User');
}
protected $table = 'news';
protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
}
2. User
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
图式
桌子users
桌子news
控制器
public function showArticle($slug)
{
$article = News::where('slug', $slug)->firstOrFail();
return view('article', compact('article'));
}
叶片
{{ $article->postedBy->name }}
当我尝试删除边栏选项卡中的名称时,它会输出 ,但是当我尝试添加 ->name 时,它会显示 nameUser' 模型。我错过了什么吗?{{ $article->postedBy }}
id
Trying to get property of non-object but I have a field
in my table and a