| <?php |
| |
| namespace App\Models; |
| |
| class Role extends AbstractModel |
| { |
| protected $table = 'roles'; |
| |
| public function users() |
| { |
| return $this->belongsToMany(User::class,'role_user', 'role_id', 'user_id') |
| ->using(RoleUser::class) |
| ->withPivot('id', 'created_at','updated_at') |
| ->withTimestamps(); |
| } |
| |
| public function roleUser() |
| { |
| return $this->hasMany(RoleUser::class,'role_id')->orderBy('id', 'desc'); |
| } |
| } |
| <?php |
| |
| namespace App\Models; |
| |
| class User extends Authenticatable |
| { |
| |
| protected $table = 'users'; |
| |
| public function roles() |
| { |
| return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id') |
| ->withTimestamps(); |
| } |
| |
| } |
| |
| <?php |
| |
| namespace App\Models; |
| |
| use Illuminate\Database\Eloquent\Relations\Pivot; |
| |
| class RoleUser extends Pivot |
| { |
| protected $table = 'role_user'; |
| |
| public function user() |
| { |
| return $this->belongsTo(User::class, 'user_id'); |
| } |
| |
| public function role() |
| { |
| return $this->belongsTo(Role::class, 'role_id'); |
| } |
| } |