User.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 'password',
  24. 'gender_id',
  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 genderId(): 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 ? __('Aktif') : __('Non Aktif'),
  55. );
  56. }
  57. public function role()
  58. {
  59. return $this->belongsTo(Role::class);
  60. }
  61. public function outlet()
  62. {
  63. return $this->belongsTo(Outlet::class);
  64. }
  65. public function scopeFilter($query, $filters)
  66. {
  67. $query->when($filters['search'] ?? null, function ($query, $search) {
  68. $query->where(function ($query) use ($search) {
  69. $query->where('name', 'like', '%' . $search . '%')
  70. ->orWhere('phone', 'like', '%' . $search . '%')
  71. ->orWhere('email', 'like', '%' . $search . '%');
  72. });
  73. });
  74. }
  75. public function hasRole($role)
  76. {
  77. return $this->role()->where('name', $role)->first() ? true : false;
  78. }
  79. }