dongzeao5047 2014-11-27 17:39
浏览 20
已采纳

显示基于用户角色和当前用户的图标

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) "wstevens@gmail.com"
            ["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) "pearlie17@yahoo.com"
            ["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
                }
        }
}
  • 写回答

1条回答 默认 最新

  • duanlie1298 2014-11-28 03:32
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置