Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中将条件表达式调整为复选框_Php_Html_Laravel_If Statement_Checkbox - Fatal编程技术网

Php 在Laravel中将条件表达式调整为复选框

Php 在Laravel中将条件表达式调整为复选框,php,html,laravel,if-statement,checkbox,Php,Html,Laravel,If Statement,Checkbox,此代码使用Laravel 5.4v.,还调整了空间/Laravel权限 我的目标是在复选框中包含条件表达式 如何实现用户是否为“超级”用户? 如果用户没有“超级”权限,则希望使“超级”复选框不可见 下面的代码不起作用 还有一种方式也很受欢迎:) @foreach($roles作为$role) {{Form::checkbox('roles[]',$role->id,$user->roles,array({!!$role=='Super'?“'style'=>'display:none'”:“

此代码使用Laravel 5.4v.,还调整了
空间/Laravel权限

我的目标是在复选框中包含条件表达式

如何实现用户是否为“超级”用户? 如果用户没有“超级”权限,则希望使“超级”复选框不可见

下面的代码不起作用

还有一种方式也很受欢迎:)


@foreach($roles作为$role)
{{Form::checkbox('roles[]',$role->id,$user->roles,array({!!$role=='Super'?“'style'=>'display:none'”:“!!}))}
{{Form::label($role->name,ucfirst($role->name))}

@endforeach
[已解决]

我自己找到的答案如下。

@role('Super')
<div class='form-group'>
    @foreach ($roles as $role) 
        {{ Form::checkbox('roles[]', $role->id, $user->roles ) }} 
        {{ Form::label($role->name, ucfirst($role->name)) }}
    <br> 
    @endforeach
</div>


@else
<div class='form-group'>
    @foreach ($roles as $role) 
        @if($role->name != 'Super')
        {{ Form::checkbox('roles[]', $role->id, $user->roles ) }} 
        {{ Form::label($role->name, ucfirst($role->name)) }}
        @endif
   <br> 
    @endforeach
</div>
@endrole
@role('Super'))
@foreach($roles作为$role)
{{Form::checkbox('roles[]',$role->id,$user->roles)}
{{Form::label($role->name,ucfirst($role->name))}

@endforeach @否则 @foreach($roles作为$role) @如果($role->name!=“Super”) {{Form::checkbox('roles[]',$role->id,$user->roles)} {{Form::label($role->name,ucfirst($role->name))} @恩迪夫
@endforeach @端角色
试试这个,但下次给我们一些有效的代码(无条件)和无效的代码

<div class='form-group'>
@foreach ($roles as $role)
 @if ( $role->name == 'super' )
 @if($user->hasRole('super'))
{{ Form::checkbox('roles[]', $role->id, $user->roles ) }} 
{{ Form::label($role->name, ucfirst($role->name)) }}
<br> 
@endif
 @else
 {{ Form::checkbox('roles[]', $role->id, $user->roles ) }} 
{{ Form::label($role->name, ucfirst($role->name)) }}
@endif
@endforeach
</div>

@foreach($roles作为$role)
@如果($role->name=='super')
@如果($user->hasRole('super'))
{{Form::checkbox('roles[]',$role->id,$user->roles)}
{{Form::label($role->name,ucfirst($role->name))}

@恩迪夫 @否则 {{Form::checkbox('roles[]',$role->id,$user->roles)} {{Form::label($role->name,ucfirst($role->name))} @恩迪夫 @endforeach
指出三个错误:

  • 首先清除语法错误,blade语句中不能有胡须:

    {{ Form::checkbox('roles[]', $role->id, $user->roles, array($role == 'Super' ? "'style' => 'display:none'" : " ")) }}
    
  • $role=='Super'
    将始终失败,因为
    $role
    是一个对象。您需要调用相应的属性。让我们改用
    $role->name

  • 你的阵列很乱。如果测试通过,数组将如下所示:
    [0=>“'style'=>'display:none'”

    您要做的是仅在测试通过时使用数组:

    $role->name == 'Super' ? ['style' => 'display:none;'] : null
    
因此,我们:

{{ Form::checkbox('roles[]', $role->id, $user->roles, $role->name == 'Super' ? ['style' => 'display:none;'] : null) }}
  • 最后,第三个参数必须是布尔值。使用您想要的方式检查用户是否具有相应的角色

也许$user->hasRole('Super')&&$role=='Super'?'禁用“:”?您能告诉我更详细的调整方法吗?不,您不需要其他语句,如果(条件){show}并且感谢-1,我试图帮助,我的代码是正确的^^您的代码使其不可见所有如果用户不是“超级”,请再次检查我的问题。谢谢。
ErrorException
不能将标量值用作数组
;(修正了三元运算符,抱歉。
{{ Form::checkbox('roles[]', $role->id, $user->roles, $role->name == 'Super' ? ['style' => 'display:none;'] : null) }}