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