简体   繁体   中英

Laravel Auth::user()->roles()->get() is empty

I was starting on learning some Laravel. I am working on a website where you can create an account, which can have roles, and depending on what roles you have, you can do stuff. What it is isn't important, but let me explain the whole situation.

I do have 3 tables:

  • One called users , which stores information about all users.
  • One called roles , which stores information about all roles (just id + name )
  • One called user_roles , which stores which roles users have. It has an id column (for PK), an user_id column (as FK to users . id ) and a role_id column (as FK to roles . id )

I do have 2 models:

Model Users:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'mood'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
     protected $hidden = [
         'password', 'remember_token',
     ];

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

Model Roles:

class Role extends Model
{
    protected $table = 'roles';

    protected $fillable = ['role'];
}

Now, what I did in one of my controller is add the following line:

var_dump(Auth::user()->roles()->get())

So I got my user, I am logged in, I did add a role and I did add a new user_roles row (with my user ID and the ID of the role I added). However, the var_dump still returns an empty array instead of the role I have. I don't understand what I did wrong

User - Role is an M:M relationship. One user can have many roles; and also, one role can be used by many users.

For many-to-many relationships Laravel (Eloquent) provides the belongsToMany method. Here is the docs: https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

Therefore, you need to replace this method in your User class:

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

with this:

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

Also, you need to tell Laravel what name the pivot table has. You can either pass it as a second argument to the belongsToMany method, or just name it roles_users and Laravel will figure it out (how does it do that is also in the docs).

This is not how belongsTo works, you need to use different relation method because belongsTo checks for "roles_id" column in User model which does not exist.

hasManyThrough should do the trick iirc

I am not gonna provide you direct solution because it would do more charm rather than help, but you need to read documentation at https://laravel.com/docs/5.6/eloquent-relationships

Documentation has example how to get country posts through user table somewhere in the middle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM