samedi 2 avril 2016

Laravel 5.0 How to get a value on a pivot table

I am an absolute beginner of laravel.

I would like to get "role_id" on the pivot table or "admin", but I don't know how. I would like to make a system that shows a different interface based on what role the user has. So I would like to distinguish users from 'admin', 'instructor', and 'user' by getting "role_id" on the pivot table.

Any advices would be appreciated. Thanks in advance

Users.php

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

Roles.php

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

Role table:

1|admin|2016-04-02 16:51:25|2016-04-02 16:51:25
2|instructor|2016-04-02 16:51:25|2016-04-02 16:51:25
3|student|2016-04-02 16:51:25|2016-04-02 16:51:25

I would like to get role_id on the pivot table.

{
"id": "1",
"name": "admin",
"created_at": "2016-04-02 16:51:25",
"updated_at": "2016-04-02 16:51:25",

 "pivot": {
     "user_id": "1",
     "role_id": "1",
     "created_at": "2016-04-02 16:54:06",
     "updated_at": "2016-04-02 16:54:06"
  }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire