User model with roles relationship.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
In controller get users with roles
$users = User::with('roles')->get();
In view show just role name for each user
@foreach($users as $user)
<div class="col-md-6 col-lg-3 ftco-animate">
<div class="staff">
<div class="img-wrap d-flex align-items-stretch">
<div class="img align-self-stretch"
style="background-image: url(images/staff/{{ $user->img }}); background-size: 250px 320px"></div>
</div>
<div class="text d-flex align-items-center pt-3 text-center">
<div>
<h3 class="mb-5">{{ $user->name }}</h3>
<span class="position mb-4">{{ $user->roles->implode('name', ', ') }}</span>
</div>
</div>
</div>
</div>
@endforeach
Docs: https://laravel.com/docs/8.x/collections#method-implode