Laravel Blade检查用户角色 [英] Laravel Blade check user role

查看:104
本文介绍了Laravel Blade检查用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel Blade模板中,我们可以使用以下代码排除HTML的某些部分:

In laravel Blade templating we can exclude some parts of HTML with this code:

        @if (Auth::user())
            <li><a href="{{ url('/home') }}">Mein Profil</a></li>
            <li><a href="{{ url('/admin') }}">Admin</a></li>
        @else
            <li><a href="{{ url('/home') }}">Mein Profil</a></li>
        @endif

如果用户通过了身份验证,则显示主页和管理员链接;如果用户未通过身份验证,则仅显示主页链接.

If user is authenticated then show home and admin links and if user is not authenticated then show only home link.

我的问题是,如果用户是管理员,如何在此处进行检查?

My question is how to make a check here if user is admin?

我有来自laravel的默认登录系统,我刚刚在表用户中添加了另外一列->('admin'),其tinyint值为1,并且在此视频中 https://www.youtube.com/watch?v=tbNKRr97uVs 我找到了用于检查用户是否为管理员的代码

I have default login system from laravel and i just added one more column in table users -> ('admin') with tinyint value 1 and in this video https://www.youtube.com/watch?v=tbNKRr97uVs i found the code for checking if user is admin

 if (!Auth::guest() && Auth::user()->admin )

,它可以在AdminMiddleware.php中工作 但它在刀片服务器中不起作用.如何使它正常工作?

and it works in AdminMiddleware.php but it doesn't work in blade. How to make this working??

推荐答案

            @if (!Auth::guest() && Auth::user()->admin)
                <li><a href="{{ url('/home') }}">Mein Profil</a></li>
                <li><a href="{{ url('/admin') }}">Admin</a></li>
            @else
                <li><a href="{{ url('/home') }}">Mein Profil</a></li>
            @endif      

这只能被清除(只需在用户表中再添加一列tinyint'admin'并将其设置为1)

this works just to be cleared (just add one more column tinyint 'admin' in user table and set to 1)

这篇关于Laravel Blade检查用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆