Laravel:在if-else条件下扩展不同的布局3个角色

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 15:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定