'date', 'date_start_operation' => 'date', 'inuavs' => 'array' ]; /** * Map attribute names to enum classes. * * @var array */ protected $enumCasts = [ 'civility' => BreederCivilty::class ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'cafel_code', 'ede_code', 'civility', 'lastname', 'firstname', 'birth_date', 'postcode', 'city', 'date_start_operation', 'inuavs', 'exploitation', 'aboutyou', 'vocation', 'likeinjob', 'loue_means', 'path_picture_1', 'path_picture_2', 'marking_egg', 'marking_b1', 'marking_b2' ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = ['questions']; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // * EVENT CLOSURES. static::creating(function (self $model) { self::areInuavCodesUniques($model); self::isEdeCodeUnique($model); }); static::updating(function (self $model) { if ($model->getRawOriginal('path_picture_1') !== $model->path_picture_1) { Storage::delete(\public_path($model->getRawOriginal('path_picture_1'))); } if ($model->getRawOriginal('path_picture_2') !== $model->path_picture_2) { Storage::delete(\public_path($model->getRawOriginal('path_picture_2'))); } self::areInuavCodesUniques($model); self::isEdeCodeUnique($model); }); static::deleting(function (self $model) { if ($model->getRawOriginal('path_picture_1')) { Storage::delete(\public_path($model->getRawOriginal('path_picture_1'))); } if ($model->getRawOriginal('path_picture_2')) { Storage::delete(\public_path($model->getRawOriginal('path_picture_2'))); } }); } // * METHODS /** * Questions attribute accessor * * @return boolean */ public function getQuestionsAttribute(): bool { if ($this->aboutyou != null && $this->aboutyou != '') { return true; } if ($this->vocation != null && $this->vocation != '') { return true; } if ($this->likeinjob != null && $this->likeinjob != '') { return true; } if ($this->loue_means != null && $this->loue_means != '') { return true; } return false; } /** * Check if inuav codes are unique * * @param self $breeder * @return boolean * @throws \Illuminate\Validation\ValidationException If inuav code is already used. */ protected static function areInuavCodesUniques(self $breeder): bool { $q = self::query(); // * Update condition $q->where(function ($q) use ($breeder) { foreach ($breeder->inuavs as $inuav) { $q->orWhere('inuavs', '=', $inuav); } }); if ($breeder->cafel_code) { $q->where('cafel_code', '!=', $breeder->cafel_code); } if ($q->count()) { throw ValidationException::withMessages([ 'inuavs' => 'Les codes INUAVS doinvent ĂȘtre uniques : ' . collect($breeder->inuavs)->implode(',') ]); } return true; } /** * Check if ede code is unique * * @param self $breeder * @return boolean * @throws \Illuminate\Validation\ValidationException If ede code is already used. */ protected static function isEdeCodeUnique(self $breeder): bool { Validator::make([ 'ede_code' => $breeder->ede_code ], ['ede_code' => [ Rule::unique($breeder->getTable(), 'ede_code')->ignore($breeder->id) ]])->validate(); return true; } }