duankui6150 2016-08-25 08:37
浏览 58
已采纳

仅启用管理员以在Laravel中查看页面

I'm trying to enable only admin users to be able to view certain contents of a navbar in my laravel project. I have a users table with a role column consisting of 'admin' and 'user' values which I use in my middleware to allow admin users to access an admin panel for CRUD operations.

I've tried:

@if(auth()->check())
 @if(Auth::user()->get(array('users.role')) == 'admin')

  'THIS IS WHAT I WANT ONLY ADMIN USERS TO SEE!'

 @endif
@endif

Yet, when logged into an 'admin' user, I still can't see admin-only content. I'm fairly new to the framework, but if anyone has any advice I would highly appreciate it!

Thanks,

Sam

  • 写回答

2条回答 默认 最新

  • drp935159 2016-08-25 08:39
    关注

    Try this:

    @if (Auth::user() && Auth::user()->role == 'admin')
        'THIS IS WHAT I WANT ONLY ADMIN USERS TO SEE!'
    @endif
    

    Auth::user() is the same as Auth::check() and it will check if user is authenticated. Only if user is authenticated, Laravel will run Auth::user()->role == 'admin' which will check if user is an admin.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)