Student.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. use Illuminate\Validation\ValidationException;
  10. /**
  11. * @property int $id
  12. * @property string $name
  13. * @property \Illuminate\Support\Carbon|null $birth_date
  14. * @property \App\ValueObjects\Cpf|null $document_number
  15. * @property string|null $gender
  16. * @property string|null $email
  17. * @property string|null $phone
  18. * @property string|null $postal_code
  19. * @property string|null $street
  20. * @property string|null $address_number
  21. * @property string|null $neighborhood
  22. * @property int|null $state_id
  23. * @property string|null $complement
  24. * @property string|null $payer_name
  25. * @property string|null $how_did_you_know_us
  26. * @property string|null $notes
  27. * @property string $status
  28. * @property \Illuminate\Support\Carbon|null $created_at
  29. * @property \Illuminate\Support\Carbon|null $updated_at
  30. * @property \Illuminate\Support\Carbon|null $deleted_at
  31. * @property int $unit_id
  32. * @property int|null $city_id
  33. * @property string|null $photo_url
  34. * @property-read \App\Models\City|null $city
  35. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentContract> $contracts
  36. * @property-read int|null $contracts_count
  37. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\StudentResponsible> $responsibles
  38. * @property-read int|null $responsibles_count
  39. * @property-read \App\Models\State|null $state
  40. * @property-read \App\Models\Unit $unit
  41. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student newModelQuery()
  42. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student newQuery()
  43. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student onlyTrashed()
  44. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student query()
  45. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereAddressNumber($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereBirthDate($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereCityId($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereComplement($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereCreatedAt($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereDeletedAt($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereDocumentNumber($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereEmail($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereGender($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereHowDidYouKnowUs($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereId($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereName($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereNeighborhood($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereNotes($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student wherePayerName($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student wherePhone($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student wherePhotoUrl($value)
  62. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student wherePostalCode($value)
  63. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereStateId($value)
  64. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereStatus($value)
  65. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereStreet($value)
  66. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereUnitId($value)
  67. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student whereUpdatedAt($value)
  68. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student withTrashed(bool $withTrashed = true)
  69. * @method static \Illuminate\Database\Eloquent\Builder<static>|Student withoutTrashed()
  70. * @mixin \Eloquent
  71. */
  72. class Student extends Model
  73. {
  74. use HasFactory, SoftDeletes;
  75. private ?array $responsibleForCreation = null;
  76. protected $table = 'students';
  77. protected $guarded = ['id'];
  78. protected $casts = [
  79. 'birth_date' => 'date',
  80. 'document_number' => Cpf::class,
  81. 'created_at' => 'datetime',
  82. 'updated_at' => 'datetime',
  83. 'deleted_at' => 'datetime',
  84. ];
  85. protected static function booted(): void
  86. {
  87. static::creating(function (Student $student): void {
  88. if ($student->isMinor() && empty($student->responsibleForCreation)) {
  89. throw ValidationException::withMessages([
  90. 'responsible' => __('validation.minor_requires_responsible'),
  91. ]);
  92. }
  93. });
  94. }
  95. public function withResponsibleForCreation(?array $responsible): self
  96. {
  97. $this->responsibleForCreation = $responsible;
  98. return $this;
  99. }
  100. public function isMinor(): bool
  101. {
  102. return $this->birth_date !== null && $this->birth_date->age < 18;
  103. }
  104. public function contracts(): HasMany
  105. {
  106. return $this->hasMany(StudentContract::class);
  107. }
  108. public function responsibles(): HasMany
  109. {
  110. return $this->hasMany(StudentResponsible::class);
  111. }
  112. public function unit(): BelongsTo
  113. {
  114. return $this->belongsTo(Unit::class, 'unit_id');
  115. }
  116. public function city(): BelongsTo
  117. {
  118. return $this->belongsTo(City::class, 'city_id');
  119. }
  120. public function state(): BelongsTo
  121. {
  122. return $this->belongsTo(State::class, 'state_id');
  123. }
  124. }