123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. class User extends Authenticatable implements MustVerifyEmail
  10. {
  11. use HasApiTokens, HasFactory, Notifiable;
  12. /**
  13. * The attributes that are mass assignable.
  14. *
  15. * @var array<int, string>
  16. */
  17. protected $fillable = [
  18. 'name',
  19. 'phone',
  20. 'email',
  21. 'status',
  22. 'password',
  23. 'gender_id',
  24. 'role_id',
  25. 'outlet_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. ];
  44. protected function genderId(): Attribute
  45. {
  46. return Attribute::make(
  47. get:fn($value) => $value == 1 ? __('words.female') : __('words.male'),
  48. );
  49. }
  50. protected function status(): Attribute
  51. {
  52. return Attribute::make(
  53. get:fn($value) => $value ? __('words.active') : __('words.not_active'),
  54. );
  55. }
  56. public function role()
  57. {
  58. return $this->belongsTo(Role::class);
  59. }
  60. public function outlet()
  61. {
  62. return $this->belongsTo(Outlet::class);
  63. }
  64. public function expenses()
  65. {
  66. return $this->hasMany(Expense::class);
  67. }
  68. public function transactions()
  69. {
  70. return $this->hasMany(Transaction::class);
  71. }
  72. public function scopeFilter($query, array $filters)
  73. {
  74. $query->when($filters['search'] ?? null, function ($query, $search) {
  75. $query->where(function ($query) use ($search) {
  76. $query->where('name', 'like', '%' . $search . '%')
  77. ->orWhere('phone', 'like', '%' . $search . '%')
  78. ->orWhere('email', 'like', '%' . $search . '%');
  79. });
  80. });
  81. }
  82. public function hasRole($role)
  83. {
  84. return $this->role()->where('name', $role)->first() ? true : false;
  85. }
  86. }