Reputation: 1589
I have a page that lists all the users from my user database table. I am currently looping through my users array and outputting each user's object property in the corresponding table field. In the last field it displays the actions for the logged in user to have access to. I need some help modifying this a bit for the additional needs I have.
I currently have it to where if the current logged in user and then user in the array being displayed are the same then it won't display the icons that way they can't accidentally delete themselves. The edit icon I don't want displayed because they can just go to their profile and edit themselves that way anyway. This I would like to keep.
What I'm wanting to do is take this field and if the current logged in user and the user in the array being displayed have the same role id then it CAN NOT display the action icons. To explain this if the current logged in user is an admin and the user in the array to be displayed is also an admin they will not be able to delete that user.
ALSO if the current logged in user has a role id lower than than the user in the array being displayed then it ALSO does not display the actions. To explain this I will use the following example. If the current logged in user only has a role id of 4 (admin) they will not be able to delete user's that have the role id of 5 (owner).
<tbody>
@foreach($users as $user)
<tr>
<td class="center">{{ $user->id }}</td>
<td>{{ $user->getFullName() }}</td>
<td>{{ $user->email_address }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->role->role_name }}</td>
<td>{{ $user->status->status_name }}</td>
<td class="center">
@if ( $user->id != Auth::id())
<a data-original-title="Edit" href="{{ route('backstage.users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
<a data-original-title="Delete" href="{{ route('backstage.users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips js-ajax-delete"><i class="fa fa-trash-o"></i></a>
@endif
</td>
</tr>
@endforeach
</tbody>
Here is the preformatted array of user objects. I have only included to dummy users.
array(51) {
[0]=>
array(13) {
["id"]=> string(1) "1"
["first_name"]=> string(7) "Will"
["last_name"]=> string(8) "Stevens"
["username"]=> string(10) "wstevens"
["email_address"]=> string(20) "[email protected]"
["avatar"]=> string(10) "wstevens"
["role_id"]=> string(1) "4"
["status_id"]=> string(1) "1"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
["role"]=>
array(5) {
["id"]=> string(1) "4"
["role_name"]=> string(5) "Owner"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
}
["status"]=>
array(5) {
["id"]=> string(1) "1"
["status_name"]=> string(6) "Active"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
}
}
[1]=>
array(13) {
["id"]=> string(1) "2"
["first_name"]=> string(6) "Furman"
["last_name"]=> string(8) "O'Reilly"
["username"]=> string(12) "wyman.haylie"
["email_address"]=> string(19) "[email protected]"
["avatar"]=> string(8) "ikyyyhzn"
["role_id"]=> string(1) "2"
["status_id"]=> string(1) "2"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
["role"]=>
array(5) {
["id"]=> string(1) "2"
["role_name"]=> string(6) "Editor"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
}
["status"]=>
array(5) {
["id"]=> string(1) "2"
["status_name"]=> string(8) "Inactive"
["created_at"]=> string(19) "2014-11-26 22:27:38"
["updated_at"]=> string(19) "2014-11-26 22:27:38"
["deleted_at"]=> NULL
}
}
}
Upvotes: 0
Views: 410
Reputation: 448
You need to change the condition of your @if
statement to:
@if($user->role['id'] < Auth::user()->role['id']) {
<a data-original-title="Edit" href="{{ route('backstage.users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
<a data-original-title="Delete" href="{{ route('backstage.users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips js-ajax-delete"><i class="fa fa-trash-o"></i></a>
@endif
Upvotes: 1