phplaravellaravel-admin

header links by role laravel-admin


I use https://laravel-admin.org/docs/en/README on my site.

I have a general header in the admin panel, which displays all the sections that can be edited, here is the code:

<?php foreach(Admin::menuLinks() as $link) { ?>
<?php if (in_array($link['uri'], ['users', 'posts', 'comments'])) {?> 
    <a class="sidebar-toggle sidebar-toggle-link" role="button" href="<?= admin_url($link['uri']) ?>"><i class="fa <?= $link['icon'] ?>"></i>
        <?= admin_trans ($link['title']) ?>
    </a>
<?php } ?>
<?php } ?>

I won’t throw all the header code, since it’s standard, there is only a code for links to sections, but if necessary I can throw it off.

So, I have 3 partitions. Each section will have its own admin, who will have a corresponding role. Now all admins with roles see all links in the header, but I need to hide them and make sure that the admin sees only the link to which he has a role with rights. How can I do such a check in the header? To display these links depending on the user's role.

I didn't find anything similar in the documentation.

There is supposedly how to get a user role, but how can I use it here Admin::user()->roles;


Solution

  • Try this

    if you want to show link by role

    @foreach(Admin::menuLinks() as $link)
        @if (Admin::user()->isRole('developer'))
            //if user role is developer
        @endif
    @endforeach
    

    if you want to show the link by permission

    @foreach(Admin::menuLinks() as $link)
        @if (Admin::user()->can('create-post'))
            //if user has permission to create post
        @endif
    @endforeach
    

    check the documentation for more details https://laravel-admin.org/docs/en/permission