label); // * TRANSFORMATIONS $model->slug = Str::slug($model->label); }); static::updating(function (self $model) { // * ASSERTIONS static::assertTitleIsUnique($model->label, $model->id); // * TRANSFORMATIONS $model->slug = Str::slug($model->label); }); static::deleting(function (self $model) { if ($model->slides()->count()) { throw ValidationException::withMessages([ 'Impossible de supprimer le slider car des slides y sont associƩs' ]); } }); } // * RELATIONSHIPS /** * hasMany slides relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function slides() { return $this->hasMany(Slide::class, 'slider_id')->orderBy('order', 'ASC'); } // * METHODS /** * Asserts using validation that the label is unique * * @param mixed $label * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If an actu title already exists. */ private static function assertTitleIsUnique($label, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } /** * Getter showable_slides filtered slides relation by showable date * * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Slide> */ public function getShowableSlidesAttribute() { $now = Carbon::now(); return $this->slides()->where(function ($q) use ($now) { $q->whereDate('publish_at', '<=', $now) ->orWhereNull('publish_at'); })->where(function ($q) use ($now) { $q->whereDate('unpublish_at', '>=', $now) ->orWhereNull('unpublish_at'); })->orderBy('order', 'ASC')->get(); } }