User.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\BikeManage\Bike;
  4. use App\Repositories\Models\Model;
  5. use Illuminate\Auth\Authenticatable;
  6. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  7. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Laravel\Lumen\Auth\Authorizable;
  10. use Spatie\Permission\Traits\HasRoles;
  11. use Tymon\JWTAuth\Contracts\JWTSubject;
  12. class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  13. {
  14. use Authenticatable, Authorizable, HasFactory, HasRoles;
  15. /**
  16. * @var string
  17. */
  18. protected $table = 'base_admins';
  19. protected $guarded = [];
  20. /**
  21. * The attributes excluded from the model's JSON form.
  22. *
  23. * @var array
  24. */
  25. protected $hidden = [
  26. 'password'
  27. ];
  28. protected $casts = [
  29. 'extra_fields' => 'json'
  30. ];
  31. /**
  32. * Get the identifier that will be stored in the subject claim of the JWT.
  33. *
  34. * @return mixed
  35. */
  36. public function getJWTIdentifier()
  37. {
  38. return $this->getKey();
  39. }
  40. /**
  41. * Return a key value array, containing any custom claims to be added to the JWT.
  42. *
  43. * @return array
  44. */
  45. public function getJWTCustomClaims()
  46. {
  47. return ['role' => 'user'];
  48. }
  49. protected static function boot()
  50. {
  51. parent::boot(); // TODO: Change the autogenerated stub
  52. self::saved(function (User $model) {
  53. if ($model->type) {
  54. Admin::query()->find($model->id)->syncRoles($model->type);
  55. }
  56. });
  57. }
  58. public function department()
  59. {
  60. return $this->belongsTo(Department::class)->select(['id', 'name']);
  61. }
  62. public function bikes()
  63. {
  64. return $this->hasMany(Bike::class, 'user_id');
  65. }
  66. }