简体   繁体   English

如何在 Laravel 中检查身份验证用户角色?

[英]How can I check auth user role in laravel?

I have role column in users table, and I want to check the value like this in the blade file :我在用户表中有角色列,我想在刀片文件中检查这样的值:

 @if ( {{Auth::user()->role }} == '1')
 // do something
 @endif

Is it possible ?是否可以 ?

In blade files, you need to write plain PHP into the @if and others blade statements.在刀片文件中,您需要将纯 PHP 写入@if和其他刀片语句。 So you would need to remove the {{ }} :所以你需要删除{{ }}

@if ( auth()->user()->role == 1)
 // do something
@endif

I think you can extend a blade.我认为你可以延长刀片。

https://laravel.com/docs/5.3/blade#extending-blade https://laravel.com/docs/5.3/blade#extending-blade

It's cool and convenient.这很酷很方便。

Latest version of Laravel will work with like this.最新版本的 Laravel 将像这样工作。 You don't need to use {{}} here.您无需在此处使用{{}}

 @if ( Auth::user()->role == 1)
 // do something
 @endif
@if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin')  == 'Admin')

// do something

@endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM