$departaimantsCollections */ class Customer extends Authenticatable implements MustVerifyEmail { use HasFactory; use Notifiable; use Prunable; use SoftDeletes; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // * EVENT CLOSURES. self::creating(function (self $model) { self::assertEmailIsUnique($model->id, $model->email); }); self::updating(function (self $model) { self::assertEmailIsUnique($model->id, $model->email); }); self::deleted(function (self $model) { if ($model->forceDeleting) { $model->notify(new DestroyedCustomer()); } else { $model->notify(new DestroyCustomer()); } }); self::restored(function () { \session()->flash( 'warning', \trans('Votre compte à été réactivé.') ); }); } // * RELATIONSHIPS /** * Get all of the departaimant collections for the Customer * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function departaimantsCollections(): HasMany { return $this->hasMany(DepartaimantsCollection::class); } // * METHODS /** * Asserts using validation that the email is unique * * @param mixed $id * @param mixed $email * @return void * @throws \Illuminate\Validation\ValidationException If user email as already used. */ private static function assertEmailIsUnique(mixed $id, mixed $email): void { self::assertFieldIsUnique($id, 'email', $email); } /** * Asserts that the field is unique * * @param mixed $id * @param string $field * @param mixed $value * @return void * @throws \Illuminate\Validation\ValidationException If the field is not unique. */ private static function assertFieldIsUnique(mixed $id, string $field, mixed $value): void { Validator::make([$field => $value], [ $field => Rule::unique('customers', $field)->ignore($id), ], [ $field . '.unique' => trans('La valeur :value pour le champ :attribute est déjà utilisé', [ 'attribute' => $field, 'value' => $value ]) ])->validate(); } // * --- AUTH --- /** * Send the email verification notification. * * @return void */ public function sendEmailVerificationNotification() { $this->notify(new VerifyEmail()); } /** * Send the password reset notification. * * @param mixed $token * @return void */ public function sendPasswordResetNotification(mixed $token) { /** @var string $token */ $this->notify(new ResetPassword($token)); } // * METHODS /** * Get all user Departaimant collection or create them if they dont exists * * @return \Illuminate\Support\Collection<\App\Models\DepartaimantsCollection> */ public function getOrCreateAllDepartaimentsCollections(): Collection { $collections = $this->departaimantsCollections()->get() ->keyBy(fn (DepartaimantsCollection $el) => $el->type->value()); foreach (DepartaimantCollectionType::all() as $depColectionType) { if (!$collections->has($depColectionType->value())) { $depCollection = new DepartaimantsCollection([ 'type' => $depColectionType->value ]); $depCollection->customer()->associate($this); $depCollection->save(); $collections->add($depCollection); } } return $collections; } }