I have a list of users, each user has their 'role' (role / function inside the system, such as 'user' and 'admin') and this role has to be shown in the users list screen, which I I did it 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
The error occurs with this line <td>{{ $user->roles[0]->name }}</td>
, indicating that 'roles' does not have index' 0 ', but when I do a dump {{ dd($user->roles[0]->name) }}
, it returns me the variable normally (a string with the name' role 'of the user).
If I try to access using 'role' using {{ $user->roles()->first()->name }}
the case is similar. With dd()
working normally, but in @foreach
of listing there is an error. The only difference is the type of error returned: 'Trying to get property 'name' of non-object '
.
What could be causing this?