Admin.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Authenticate\MustVerifyEmail;
  4. use DateTimeInterface;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use Illuminate\Notifications\Notifiable;
  8. use Laravel\Sanctum\HasApiTokens;
  9. use Tymon\JWTAuth\Contracts\JWTSubject;
  10. class Admin extends Authenticatable implements JWTSubject
  11. {
  12. use HasApiTokens, HasFactory, Notifiable;
  13. // use SoftDeletes;
  14. // 设置表名
  15. protected $table = 'admin';
  16. // 设置主键
  17. // public $primaryKey = 'id';
  18. /**
  19. * 自动生成时间戳
  20. */
  21. // public $timestamps = FALSE;
  22. // const CREATED_AT = 'create_time';
  23. // const UPDATED_AT = 'update_time';
  24. // 自定义软删除字段 默认 deleted_at
  25. // const DELETED_AT = 'delete_at';
  26. /**
  27. * 日期时间的存储格式
  28. *
  29. * @var string
  30. */
  31. // protected $dateFormat = 'Y-m-d H:i:s';
  32. /**
  33. * The attributes that are mass assignable.
  34. *
  35. * @var array<int, string>
  36. */
  37. protected $fillable = [];
  38. /**
  39. * The attributes that should be hidden for serialization.
  40. *
  41. * @var array<int, string>
  42. */
  43. protected $hidden = [
  44. 'password',
  45. 'remember_token',
  46. ];
  47. /**
  48. * The attributes that should be cast.
  49. *
  50. * @var array<string, string>
  51. */
  52. protected $casts = [
  53. 'create_at' => 'datetime', //'datetime:Y-m-d H:i:s'
  54. 'password' => 'hashed',
  55. ];
  56. protected $appends = ['roles'];
  57. public function getRolesAttribute()
  58. {
  59. return AdminRole::where(['admin_id' => $this->id])->pluck('id');
  60. }
  61. protected function serializeDate(DateTimeInterface $date): string
  62. {
  63. return $date->format('Y-m-d H:i:s');
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. public function getJWTIdentifier()
  69. {
  70. return $this->getKey();
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. public function getJWTCustomClaims(): array
  76. {
  77. return [];
  78. }
  79. }