User.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use App\Repositories\Models\School\Grade;
  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. protected static function boot()
  32. {
  33. parent::boot(); // TODO: Change the autogenerated stub
  34. self::saved(function (User $model) {
  35. if ($model->type) {
  36. Admin::query()->find($model->id)->syncRoles($model->type);
  37. }
  38. });
  39. }
  40. /**
  41. * Get the identifier that will be stored in the subject claim of the JWT.
  42. *
  43. * @return mixed
  44. */
  45. public function getJWTIdentifier()
  46. {
  47. return $this->getKey();
  48. }
  49. /**
  50. * Return a key value array, containing any custom claims to be added to the JWT.
  51. *
  52. * @return array
  53. */
  54. public function getJWTCustomClaims()
  55. {
  56. return ['role' => 'user'];
  57. }
  58. public function department()
  59. {
  60. return $this->belongsTo(Department::class)->select(['id', 'name']);
  61. }
  62. public function grade()
  63. {
  64. return $this->belongsTo(Grade::class, 'grade_id')->select(['id', 'name']);
  65. }
  66. }