1
I have a list of users, each user has their 'role' (role/function within the system, such as 'user' and 'admin') and this role has to be shown on the user listing screen, which I did using the code block below.
@foreach ($users as $key => $user)
<tr class="list-users">
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->roles[0]->name }}</td>
</tr>
@endforeach
Error occurs with this line <td>{{ $user->roles[0]->name }}</td>
, indicating that roles do not have the index '0', but when I dump {{ dd($user->roles[0]->name) }}
, it returns me the variable normally ( a string with the name of the user’s role).
If I try to access using the 'role' using {{ $user->roles()->first()->name }}
the case is similar. With the dd()
works normally, but in the @foreach
an error occurs. The only difference is the type of error returned: 'Trying to get property 'name' of non-object '
.
What could be causing this?
How you’re passing $users to the page?
– sant0will
Yes. @sant0will
– Matheus de Melo .F