StudentResponsible.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Models;
  3. use App\ValueObjects\Cpf;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Illuminate\Validation\ValidationException;
  9. /**
  10. * @property int $id
  11. * @property int $student_id
  12. * @property string $name
  13. * @property \Illuminate\Support\Carbon $birth_date
  14. * @property \App\ValueObjects\Cpf $cpf
  15. * @property string|null $gender
  16. * @property string $degree
  17. * @property string $email
  18. * @property string $phone
  19. * @property string $street
  20. * @property string|null $address_number
  21. * @property string $postal_code
  22. * @property string $neighborhood
  23. * @property int $city_id
  24. * @property int $state_id
  25. * @property string|null $complement
  26. * @property string|null $notes
  27. * @property \Illuminate\Support\Carbon|null $created_at
  28. * @property \Illuminate\Support\Carbon|null $updated_at
  29. * @property \Illuminate\Support\Carbon|null $deleted_at
  30. * @property-read \App\Models\City $city
  31. * @property-read \App\Models\State $state
  32. * @property-read \App\Models\Student $student
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible newModelQuery()
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible newQuery()
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible onlyTrashed()
  36. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible query()
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereAddressNumber($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereBirthDate($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereCityId($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereComplement($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereCpf($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereCreatedAt($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereDegree($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereDeletedAt($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereEmail($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereGender($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereId($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereName($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereNeighborhood($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereNotes($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible wherePhone($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible wherePostalCode($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereStateId($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereStreet($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereStudentId($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible whereUpdatedAt($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible withTrashed(bool $withTrashed = true)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|StudentResponsible withoutTrashed()
  59. * @mixin \Eloquent
  60. */
  61. class StudentResponsible extends Model
  62. {
  63. use HasFactory, SoftDeletes;
  64. protected $table = 'student_responsibles';
  65. protected $guarded = ['id'];
  66. protected $casts = [
  67. 'cpf' => Cpf::class,
  68. 'birth_date' => 'date',
  69. 'created_at' => 'datetime',
  70. 'updated_at' => 'datetime',
  71. 'deleted_at' => 'datetime',
  72. ];
  73. protected static function booted(): void
  74. {
  75. static::saving(function (StudentResponsible $responsible): void {
  76. if ($responsible->birth_date !== null && $responsible->birth_date->age < 18) {
  77. throw ValidationException::withMessages([
  78. 'birth_date' => __('validation.responsible_must_be_adult'),
  79. ]);
  80. }
  81. });
  82. }
  83. public function student(): BelongsTo
  84. {
  85. return $this->belongsTo(Student::class);
  86. }
  87. public function city(): BelongsTo
  88. {
  89. return $this->belongsTo(City::class);
  90. }
  91. public function state(): BelongsTo
  92. {
  93. return $this->belongsTo(State::class);
  94. }
  95. }