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

general's avatar

Relationship between Users and Roles

Hi guys

I finally managed to make middleware and login. Now I want to show all users from DB and show their roles (title of roles). How can I do this? I know it's possible with long query but I hope there is some better solution in Laravel :)

I have 3 tables in DB: users, roles, role_user (pivot)

User model:

public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

Role model:

public function users()
    {
        return $this->hasMany('App\User');
    }

How can I show user's role in blade? BTW: every user can have only one role...

0 likes
15 replies
thomaskim's avatar

@general If every user can only have one role, then you don't need a pivot table since you are defining a one-to-many relationship.

Every user would have a role_id attribute. Then, you can do this:

User.php Model

public function role()
{
    return $this->belongsTo('App\Role');
}

Role.php Model

public function users()
{
    return $this->hasMany('App\User');
}

If you want to show all users from the database with their roles, you can do this:

$users = User::with('role')->get();

In your blade view...

@foreach ($users as $user)
    {{ $user->role->name }}
@endforeach
1 like
general's avatar

@thomaskim now I get some error...

ErrorException in User.php line 366: Missing argument 1 for App\User::role_id()

public function role_id($user_id)
    {
        if(!is_null($user_id))
        {
            return DB::table('role_user')->where('user_id', '=', $user_id)->first()->role_id;
        }

        return null;
    }

used in controller:

$roleActive = $user->role_id($id);
thomaskim's avatar

@general If every user will only have one role, then my suggestion to you was to follow the structure that I mentioned above. That means restructuring your database to remove the pivot table and add role_id to the users table. Your example role_id method assumes that there is a pivot table. If you did restructure your database, then you can get a user's role_id by simply doing $user->role_id.

general's avatar

@thomaskim still get some error from blade. Trying to get property of non-object

I think there is problem with this:

$users = User::with('roles')->get();

why with('roles') ???

thomaskim's avatar

@general Remember in the User model, this is the function:

public function role()
{
    return $this->belongsTo('App\Role');
}

The function name "role" is singular. You need to match that like so:

$users = User::with('role')->get();
1 like
general's avatar

@thomaskim still get same error... I understand my first version but don't understand your version because there is no specified role_id column from users table anywhere in method...

general's avatar

@thomaskim I know my english isn't best but like I said I created role_id column, used your code but still get error...

bimalshah72's avatar
Level 6

@general

@thomaskim is saying

  1. Remove pivot table and its relation ship from models
  2. Add foreign key - role_id in the users table
  3. in Role model add
public function users()
{
    return $this->hasMany('App\User');
}
  1. in User model add
public function role()
{
    return $this->belongsTo('App\Role');
}

The reason is..Single user has single role but single role can be have many users and hence hasmany relationship.

Now when you say

$users = User::with('role')->get();

It gets all Users with their role - as in User model we have added role() relationship method.

4 likes
general's avatar

@bimalshah72 one more question :)

I have created middleware:

public function handle($request, Closure $next)
    {
        if(!$this->auth->guest())
        {
            if($request->user()->hasRole('moderator') OR $request->user()->hasRole('admin')) {
                return $next($request);
            }
        }

        return redirect('admin/auth/login');
    }

hasRole:

public function hasRole($title)
    {
        if(!is_null($title))
        {
            if ($this->role->titleURL == $title) return true;
        }

        return false;
    }

titleURL is admin/moderator/user

But i'm only able to log in with moderator, not with admin. Do you know where is problem?

bimalshah72's avatar

@general Could you try I think if here are only 3 roles and for 2 roles needs to move further then best is

public function handle($request, Closure $next)
    {
        if(!$this->auth->guest())
        {
            if(!$this->auth-->user()->hasRole('user')) {
                return $next($request);
            }
        }

        return redirect('admin/auth/login');
    }
ak1993's avatar

can anyone help me, about, how to do log in system with role, user and permission table including one to many relationship? i want to make two type of user admin & user.

Please or to participate in this conversation.