User.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 scopeFilter($query, $filters)
  65. {
  66. $query->when($filters['search'] ?? null, function ($query, $search) {
  67. $query->where(function ($query) use ($search) {
  68. $query->where('name', 'like', '%' . $search . '%')
  69. ->orWhere('phone', 'like', '%' . $search . '%')
  70. ->orWhere('email', 'like', '%' . $search . '%');
  71. });
  72. });
  73. }
  74. public function hasRole($role)
  75. {
  76. return $this->role()->where('name', $role)->first() ? true : false;
  77. }
  78. }