[英]Laravel 5.8, Auth::user() is not using User model
I have a function in my User model which is called isAdmin, if "Admin" is set to 1 in the database, it returns true, if not it returns false.我的用户 model 中有一个 function,称为 isAdmin,如果数据库中的“Admin”设置为 1,则返回 true,否则返回 false。
How is this gonna work with Auth::user()
?这将如何与Auth::user()
一起使用?
When I do Auth::user()->isAdmin()
, it returns "Property [admin] does not exist on this collection instance."
当我执行Auth::user()->isAdmin()
时,它返回"Property [admin] does not exist on this collection instance."
Thats why I came to the conclusion it may not use the User model?这就是为什么我得出结论它可能不使用用户 model?
User model用户 model
public function isAdmin() {
if($this->admin == 1) {
return true;
} else {
return false;
}
}
public function view ()
{
if(Auth::check() && Auth::user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
If I may suggest, for this use case, you can actually make do without an extra function.如果我可以建议,对于这个用例,你实际上可以不用额外的 function。 You could just say auth()->user()->admin
, specially if the 'admin' column in the database is boolean type.你可以说auth()->user()->admin
,特别是如果数据库中的 'admin' 列是 boolean 类型。
Otherwise (even admin coloumn is not boolean type) you can set up a mutator method in the model, like so:否则(即使 admin 列不是 boolean 类型)您可以在 model 中设置一个 mutator 方法,如下所示:
public function getIsAdminAttribute()
{
return (bool) $this->admin;
}
Then to check you can access it like so: Auth::user()->isAdmin
or auth()->user()->isAdmin
然后检查您是否可以像这样访问它: Auth::user()->isAdmin
或auth()->user()->isAdmin
And better yet, you might want to read about Gate
and Policies
to achieve more robust access controlling.更好的是,您可能想了解Gate
和Policies
以实现更强大的访问控制。 https://laravel.com/docs/5.7/authorization https://laravel.com/docs/5.7/authorization
Suggestion, change the code to just this:建议,将代码更改为:
public function isAdmin() {
return $this->admin;
}
This code does exactly the same as you've got above..此代码与您上面的代码完全相同。
Now in your admin.blade.php
you are using:现在在您使用的admin.blade.php
中:
$user->isAdmin();
But in the controller you have:但是在 controller 你有:
$user = User::all();
which returns collection.返回集合。
You should iterate over it, and check on each user instance if it is an admin:您应该对其进行迭代,并检查每个用户实例是否是管理员:
$users = User::all();
In the view:在视图中:
@foreach($users as $user)
@if($user->isAdmin())
{{ $user->name }} // some code here..
@endif
@endforeach
No Need To do anything just check if login then auth()->check() is return true then auth()->user() return the user无需做任何事情只需检查登录然后 auth()->check() 是否返回 true 然后 auth()->user() 返回用户
public function view ()
{
if(auth()->check() && auth()->user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
public function isAdmin()
{
return $this->admin;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.