试图获取非对象的属性“ admin”

时间:2019-02-26 19:41:48

标签: php laravel-5

我试图避免普通用户查看仪表板中的内容,但是当我尝试对用户进行身份验证时,它显示“正在尝试获取非对象的属性'admin'” 错误在这里说什么[在此处输入图片描述] [1]

用户模型

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'admin'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function profile() {
        return $this->hasOne('App\Profile');
    }

    public function posts() {
        return $this->hasMany('App\Post');
    }
}

路线

Auth::routes();



Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){

Route::get('/home', 'HomeController@index')->name('home');

1 个答案:

答案 0 :(得分:0)

首先,您应该确保Auth::user()方法返回某些内容。

与此同时,您可以将第110行更改为:

<?php if (Auth::user() && Auth::user()->admin): ?>