php - 试图获取非对象的属性“管理员”
问题描述
我试图避免普通用户查看我的仪表板中的内容,但是当我尝试在此处对用户进行身份验证时,它显示“尝试获取非对象的属性‘管理员’”[在此处输入图像描述] [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');
解决方案
首先,您应该确保该Auth::user()
方法返回了一些东西。
同时,您可以将第 110 行更改为:
<?php if (Auth::user() && Auth::user()->admin): ?>
推荐阅读
- c# - C# 使用 Microsoft 终端服务活动客户端 (RDP) 连接到远程服务器
- typescript - 打字稿没有为“/path/to/file.ts”发出输出
- c# - 导出到 excel 返回空白表
- javascript - 解析 JSON 时出错 - 位置 450 处 JSON 中的意外标记}
- google-cloud-platform - 无法从谷歌云实例中检索文件
- android - Webview没有在android中以正确的格式显示文本
- javascript - 在 ES6 类中声明一个变量。
- c# - 删除图像的圆形透明部分 c#
- postgresql - POSTGRES:pg_cancel_backend 并不总是有效(背后的原因)
- javascript - “错误”消息:“索引”已定义但从未使用过。(没有未使用的变量)