User.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Database\Eloquent\Casts\Attribute;
  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 Spatie\Permission\Traits\HasRoles;
  10. class User extends Authenticatable
  11. {
  12. use HasApiTokens, HasFactory, Notifiable, HasRoles;
  13. protected string $guard_name = 'api';
  14. /**
  15. * The attributes that are mass assignable.
  16. *
  17. * @var array<int, string>
  18. */
  19. protected $fillable = [
  20. 'name',
  21. 'username',
  22. 'email',
  23. 'password',
  24. 'company_id',
  25. 'department_id',
  26. ];
  27. /**
  28. * The attributes that should be hidden for serialization.
  29. *
  30. * @var array<int, string>
  31. */
  32. protected $hidden = [
  33. 'password',
  34. 'remember_token',
  35. ];
  36. /**
  37. * The attributes that should be cast.
  38. *
  39. * @var array<string, string>
  40. */
  41. protected $casts = [
  42. 'email_verified_at' => 'datetime',
  43. 'password' => 'hashed',
  44. ];
  45. protected function superAdmin(): Attribute
  46. {
  47. return Attribute::make(
  48. get: fn() => $this->role_id == config("auth.super_admin_role_id"),
  49. );
  50. }
  51. public function guardName(): string
  52. {
  53. return $this->guard_name;
  54. }
  55. public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  56. {
  57. return $this->belongsTo(Company::class);
  58. }
  59. public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  60. {
  61. return $this->belongsTo(Role::class);
  62. }
  63. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  64. {
  65. return $this->belongsTo(User::class, 'created_by');
  66. }
  67. public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  68. {
  69. return $this->belongsTo(Department::class);
  70. }
  71. }