Laravel 5, authenticating and displaying user roles

Holo

I am very new to using pivot tables full stop never mind within Laravel, my apps have never been too complicated. Here is my issue.

I have setup a new test site, it has 3 tables.

users
roles
user_role

In the User model I have defined the relationship with the Role model as:

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

Ok, so this should be easy enough I thought. First of all authenticating a user is simple

Auth::user();

Keeping this very simple all I really want to do is display the role or roles the user has. If I put this in a view:

Aith::user()->roles;

I get the following result

[{"id":"7","name":"Administrator","created_at":"2015-03-19      00:14:53","updated_at":"2015-03-19 00:14:53","pivot":{"user_id":"1","role_id":"7"}}]

Now that is spot on, however I will need to sound totally daft here. How do I just get it to return the role name?

Instead of the result above I just simply want it to display:

Administrator

Nothing more.

Dallin

either this to get the first record

Auth::user()->roles->first()->name

or use a loop

@foreach(Auth::user()->roles as $role)
    {{ $role->name }}
@endforeach

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

displaying navbar according to user roles and permisions laravel

From Dev

How to make user, and roles relationship in Laravel 5

From Dev

Laravel 5 Entrust update user roles

From Dev

Issue in Authenticating the user: laravel 5.1

From Dev

Laravel 5 new auth: Get current user and how to implement roles?

From Dev

Integrating Active Directory in Windows Server with Laravel 5 for multiple user roles

From Dev

Middleware for user roles in Laravel

From Dev

Middleware for user roles in Laravel

From Dev

Authenticating a user role in Laravel and protecting a route

From Dev

User authenticating but not staying logged in Laravel 5.3

From Dev

How to get user with roles in laravel?

From Dev

Laravel 5 Displaying image

From Dev

Preventing Brute-Force Attacks When Authenticating A User in Laravel

From Dev

Laravel 5.6 - Using built in Auth on custom table - Not authenticating user/password

From Dev

MVC 5 - Roles - IsUserInRole and Adding user to role

From Dev

How to use Roles in user identity in MVC 5

From Dev

MVC 5 Identity. Roles for User

From Dev

how get just user with roles in mvc 5

From Dev

laravel route filter to check user roles

From Dev

Laravel - How to get Entrust Roles of a specific user

From Dev

Problems adding roles to a user model in Laravel 4

From Dev

get array of roles ID of a User in laravel

From Dev

Laravel - How to get Entrust Roles of a specific user

From Dev

get array of roles ID of a User in laravel

From Dev

Laravel 5 db:seed the username and password, but not authenticating on login

From Dev

Authenticating user with second login

From Dev

Authenticating a Django user with email

From Dev

authenticating user to stocktwits

From Dev

Laravel - authenticating with session token

Related Related

  1. 1

    displaying navbar according to user roles and permisions laravel

  2. 2

    How to make user, and roles relationship in Laravel 5

  3. 3

    Laravel 5 Entrust update user roles

  4. 4

    Issue in Authenticating the user: laravel 5.1

  5. 5

    Laravel 5 new auth: Get current user and how to implement roles?

  6. 6

    Integrating Active Directory in Windows Server with Laravel 5 for multiple user roles

  7. 7

    Middleware for user roles in Laravel

  8. 8

    Middleware for user roles in Laravel

  9. 9

    Authenticating a user role in Laravel and protecting a route

  10. 10

    User authenticating but not staying logged in Laravel 5.3

  11. 11

    How to get user with roles in laravel?

  12. 12

    Laravel 5 Displaying image

  13. 13

    Preventing Brute-Force Attacks When Authenticating A User in Laravel

  14. 14

    Laravel 5.6 - Using built in Auth on custom table - Not authenticating user/password

  15. 15

    MVC 5 - Roles - IsUserInRole and Adding user to role

  16. 16

    How to use Roles in user identity in MVC 5

  17. 17

    MVC 5 Identity. Roles for User

  18. 18

    how get just user with roles in mvc 5

  19. 19

    laravel route filter to check user roles

  20. 20

    Laravel - How to get Entrust Roles of a specific user

  21. 21

    Problems adding roles to a user model in Laravel 4

  22. 22

    get array of roles ID of a User in laravel

  23. 23

    Laravel - How to get Entrust Roles of a specific user

  24. 24

    get array of roles ID of a User in laravel

  25. 25

    Laravel 5 db:seed the username and password, but not authenticating on login

  26. 26

    Authenticating user with second login

  27. 27

    Authenticating a Django user with email

  28. 28

    authenticating user to stocktwits

  29. 29

    Laravel - authenticating with session token

HotTag

Archive