Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uicabpatweyler's avatar

Show name roles in a View blade with Bouncer

Hello, good day. I am starting with the use of ACL with BOUNCER. In my view blade I have something like this:

@foreach($users as $user)

    {{ $user->name }}

    {{ $user->getRoles()  }}

@Endforeach

The above shows me the name of the user and the role or roles assigned. However these are shown as follows:

[ "RoleName1", "RoleName2" ]

Inside square brackets and double quotes.

How could I show it without the square brackets and quotes? I thank you for your help.

Regards.

0 likes
5 replies
manelgavalda's avatar

It's is showing with the brackets because it's a collection, if you want to show them in commas for example you can use the implode collection method:

    {{ $user->getRoles()->implode(', ')  }}
uicabpatweyler's avatar

Thanks @manelgavalda. So can I do this?

@foreach($users as $user)

    {{ $user->name }}

    @foreach($user->getRoles()->implode(', ') as $roleName)
          {{ $roleName}}
    @endforeach

@endforeach

It is right?

manelgavalda's avatar
Level 50

You can chose between the implode or the foreach, not both at the same time:

  • If you want to show all the roles separated by commas you can use this:
    {{ $user->getRoles()->implode(', ')  }}

This will actually convert your collection ([ "RoleName1", "RoleName2" ]) to a string separated by commas (RoleName1, RoleName2).

  • The other option is to use the foreach, so you will list each individual role:
@foreach($users as $user)

    {{ $user->name }}

    @foreach($user->getRoles() as $roleName) // If you use this method remove the `implode`
          {{ $roleName}}
    @endforeach

@endforeach
uicabpatweyler's avatar

Thank you for your successful response. Use this:

@foreach ($user->getRoles() as $roleName)
    {{ $roleName }}
@endforeach 

Perfect!!!

1 like
manelgavalda's avatar

Cool, glad it worked! Please mark it as solved, so other people will know.

Please or to participate in this conversation.