首页 > 解决方案 > 试图获取非对象的属性“管理员”

问题描述

我试图避免普通用户查看我的仪表板中的内容,但是当我尝试在此处对用户进行身份验证时,它显示“尝试获取非对象的属性‘管理员’”[在此处输入图像描述] [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');

标签: phplaravel-5

解决方案


首先,您应该确保该Auth::user()方法返回了一些东西。

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

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

推荐阅读