[英]Laravel if statement on blade view
I want to filter out my users table data based on roles and split it into 2 table on the view.我想根据角色过滤掉我的用户表数据并将其拆分为视图上的 2 个表。
My Roles:我的角色:
The one table i want it to show only data of users with admin role and the other table should show data of users with entry, junior roles.我希望一张表仅显示具有管理员角色的用户的数据,而另一张表应显示具有条目、初级角色的用户的数据。
Table 1 works perfectly:表 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
Table 2 is the one that is giving me issues:表 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
I have tried following:我试过以下:
@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'))
and this one:和这个:
@if(implode (', ',$user->roles()->get()->pluck('name')->toArray()) == 'entry' || 'junior')
Returns a table with all the data.返回包含所有数据的表。
I need the table to return all the data with user roles of entry and junior.我需要该表来返回具有条目和初级用户角色的所有数据。
And i can't use the NOT EQUAL to cause there's a senior role that is catered for separately.而且我不能使用 NOT EQUAL 来导致有一个单独的高级角色。
You are making it more complex than it needs to be.你让它变得比它需要的更复杂。
$user->roles
is a collection, which means you have a lot of helper-functions at your disposal. $user->roles
是一个集合,这意味着您可以使用许多辅助函数。 All you need to do is check if the collection contains either value.您需要做的就是检查集合是否包含任何一个值。
@if ($user->roles->whereIn('name', ['entry', 'junior'])->count())
You can also simplify your working @if
with helper functions,您还可以使用辅助函数简化您的工作
@if
,
@if ($user->roles->contains('name', 'admin'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.