general
1395
14
Eloquent

Relationship between Users and Roles

Posted 3 years ago by general

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...

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.