User.php 2.0 KB

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