I want to filter out my users table data based on roles and split it into 2 table on the view.
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:
@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:
@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.
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. 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 ($user->roles->contains('name', 'admin'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.