laravel 为刀片文件中的变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47753537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assign a value to a variable in blade file
提问by user3386779
I want to assign a value to a variable in a laravel blade file based on condition.
我想根据条件为 laravel 刀片文件中的变量赋值。
<?php $status=''; ?>
@if($user_role=='2'){
<?php $status='1'; ?>
}
@elseif($user_role=='3'){
<?php $status='2'; ?>
}
@elseif($user_role=='4'){
<?php $status='3'; ?>
}
but {{status}} returns nothing.How to assign a value to a variable in laravel 5.3 blade file
但是 {{status}} 什么都不返回。如何在 laravel 5.3 刀片文件中为变量赋值
回答by Kuldeep Mishra
@if($user_role=='2')
@php $status='1'; @endphp
@endif
@if($user_role=='3')
@php $status='2'; @endphp
@endif
@if($user_role=='4')
@php $status='3'; @endphp
@endif
you can check the value by adding echo
您可以通过添加 echo 来检查该值
@if($user_role=='4')
@php echo $status='3'; @endphp
@endif
回答by rchatburn
Switch Statement would be better
Switch 语句会更好
@switch($user_role)
@case(1)
@php $status = 1;@endphp
or <h1>Status is 1</h1>
@break
@case(2)
Second case...
@break
@default
Default case...
@php $status = 5;@endphp
@endswitch
But most if not all of your logic should be done in a controller
但是大多数(如果不是全部)逻辑都应该在控制器中完成
https://laravel.com/docs/5.5/blade#switch-statements
https://laravel.com/docs/5.5/blade#switch-statements
if you don't have @switch available in your version of laravel you can always do
如果您的 Laravel 版本中没有可用的 @switch,您可以随时进行
@php
switch($user_role) {
case 1:
$status = 1;
break;
case 2:
$status = 2;
break;
default:
$status = 5;
}
@endphp