英文:
Laravel: Extending different layouts inside if-else condition 3 roles
问题
我有3个角色 用户 员工 和 管理员
我有这段代码。
@if(Auth::User()->user_role == 1)
{{$type = "layouts.admin_layout"}}
@elseif(Auth::User()->user_role == 2)
{{$type = "layouts.employee_layout"}}
@elseif(Auth::User()->user_role == 3)
{{$type = "layouts.user_layout"}}
@endif
它给我提供了3种布局。
英文:
I have 3 roles user employee and admin
I have this code.
@if(Auth::User()->user_role == 1)
{{$type = "layouts.admin_layout"}}
@elseif(Auth::User()->user_role == 2)
{{$type = "layouts.employee_layout"}}
@elseif(Auth::User()->user_role == 3)
{{$type = "layouts.user_layout"}}
@endif
It gives me 3 layout.
答案1
得分: 1
你不能使用{{ }}
来声明变量,它用于输出/打印变量。相反,尝试使用@php #在此处编写代码 @endphp
@if(Auth::User()->user_role == 1)
@php $type = "layouts.admin_layout" @endphp
@elseif(Auth::User()->user_role == 2)
@php $type = "layouts.employee_layout" @endphp
@elseif(Auth::User()->user_role == 3)
@php $type = "layouts.user_layout" @endphp
@endif
英文:
you can't declare a variable using {{ }}
. this is used for echo/print a variable. Instead {{ }}
try using @php #code here @endphp
@if(Auth::User()->user_role == 1)
@php $type = "layouts.admin_layout" @endphp
@elseif(Auth::User()->user_role == 2)
@php $type = "layouts.employee_layout" @endphp
@elseif(Auth::User()->user_role == 3)
@php $type = "layouts.user_layout" @endphp
@endif
答案2
得分: 0
@php
if(Auth::User()->user_role == 1)
$type = "layouts.admin_layout";
elseif(Auth::User()->user_role == 2)
$type = "layouts.employee_layout";
elseif(Auth::User()->user_role == 3)
$type = "layouts.user_layout";
@endphp
英文:
Try this
@php
if(Auth::User()->user_role == 1)
$type = "layouts.admin_layout";
elseif(Auth::User()->user_role == 2)
$type = "layouts.employee_layout";
elseif(Auth::User()->user_role == 3)
$type = "layouts.user_layout";
@endphp
答案3
得分: 0
在 switch 中也可以尝试使用它:
@switch(Auth::user()->user_role)
@case(1)
@php $type = "layouts.admin_layout" @endphp
@break
@case(2)
@php $type = "layouts.employee_layout" @endphp
@break
@case(3)
@php $type = "layouts.user_layout" @endphp
@endswitch
当使用多个 if/elseif/else 语句时,switch 更易读。而且你只需要调用 `Auth::user()->user_role` 一次,而不是三次。
英文:
You can also try it in a switch:
@switch(Auth::user()->user_role)
@case(1)
@php $type = "layouts.admin_layout" @endphp
@break
@case(2)
@php $type = "layouts.employee_layout" @endphp
@break
@case(3)
@php $type = "layouts.user_layout" @endphp
@endswitch
Switch tend to be a bit more readable when using multiple if/elseif/else statements. Plus you only call Auth:user()->user_role
once instead of 3 times.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论