[英]Laravel if statement on blade view
我想根据角色过滤掉我的用户表数据并将其拆分为视图上的 2 个表。
我的角色:
我希望一张表仅显示具有管理员角色的用户的数据,而另一张表应显示具有条目、初级角色的用户的数据。
表 1 完美运行:
@foreach($users as $user)
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == 'admin')
<tr>
<th scope="row">{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{ implode (', ',$user->roles()->get()->pluck('name')->toArray()) }}</td>
<td class="row" style="margin-left: 1%">
<a href="{{ route('admin.users.edit', $user->id) }}" type="button" class="btn btn-primary" style="margin-right:10px;">Edit</a>
<form action="{{ route('admin.users.destroy', $user) }}" method="POST">
@csrf
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endif
@endforeach
表 2 是给我的问题:
@foreach($users as $user)
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == ['entry' && 'junior'])
<tr>
<th scope="row">{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{ implode (', ',$user->roles()->get()->pluck('name')->toArray()) }}</td>
<td class="row" style="margin-left: 1%">
<a href="{{ route('admin.users.edit', $user->id) }}" type="button" class="btn btn-primary" style="margin-right:10px;">Edit</a>
<form action="{{ route('admin.users.destroy', $user) }}" method="POST">
@csrf
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endif
@endforeach
我试过以下:
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == ['entry', 'junior'])
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == 'entry' && 'junior')
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == 'entry', 'junior')
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == ('entry' 'junior'))
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == ('entry', 'junior'))
和这个:
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == 'entry' || 'junior')
返回包含所有数据的表。
我需要该表来返回具有条目和初级用户角色的所有数据。
而且我不能使用 NOT EQUAL 来导致有一个单独的高级角色。
你让它变得比它需要的更复杂。 $user->roles
是一个集合,这意味着您可以使用许多辅助函数。 您需要做的就是检查集合是否包含任何一个值。
@if ($user->roles->whereIn('name', ['entry', 'junior'])->count())
您还可以使用辅助函数简化您的工作@if
,
@if ($user->roles->contains('name', 'admin'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.