Student.php 5.8 KB

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