dimanche 30 août 2015

How to make user, role and user_roles relationship in laravel 5?

I have three tables :

User ->
 id : 
 name : 

Roles ->
 id :
 role_name :
 access :

user_roles->
 id :
 user_id : ->references('id')->on('users');
role_id : ->references('id')->on('roles');

I am trying to access user_roles details from user.

My user model has :

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

My role model has :

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

When I try to do following :

$user = User::find(1);

        $details = [
            'name' => $user->first_name,
            'role' => $user->role->name

        ];

I get error :

Trying to get property of non-object

How to do that?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1X21KGx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire