User.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. 'address',
  22. 'status',
  23. 'gender',
  24. 'password',
  25. 'role_id',
  26. 'outlet_id',
  27. ];
  28. /**
  29. * The attributes that should be hidden for serialization.
  30. *
  31. * @var array<int, string>
  32. */
  33. protected $hidden = [
  34. 'password',
  35. 'remember_token',
  36. ];
  37. /**
  38. * The attributes that should be cast.
  39. *
  40. * @var array<string, string>
  41. */
  42. protected $casts = [
  43. 'email_verified_at' => 'datetime',
  44. ];
  45. protected function gender(): Attribute
  46. {
  47. return Attribute::make(
  48. get:fn($value) => $value == 1 ? __('words.female') : __('words.male'),
  49. );
  50. }
  51. protected function status(): Attribute
  52. {
  53. return Attribute::make(
  54. get:fn($value) => $value ? __('words.active') : __('words.not_active'),
  55. );
  56. }
  57. public function role()
  58. {
  59. return $this->belongsTo(Role::class);
  60. }
  61. public function scopeFilter($query, $search)
  62. {
  63. $query->when($search ?? null, function ($query, $search) {
  64. $query->where(function ($query) use ($search) {
  65. $query->where('name', 'like', '%' . $search . '%')
  66. ->orWhere('phone', 'like', '%' . $search . '%')
  67. ->orWhere('email', 'like', '%' . $search . '%');
  68. });
  69. });
  70. }
  71. }