$subranges * Subranges Has-Many relationship. */ class Range extends Model implements SeoInterface, Sitemapable, RedirectableInterface { use HasFactory; use SeoTrait; use IsRedirectable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'label', 'slug', 'seo_title', 'seo_description', 'color', 'picture', 'picture_alt', 'picture_title', 'picture_banner', 'picture_banner_alt', 'picture_banner_title', 'order', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'color' => RgbaColor::class, ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function () { throw new \RuntimeException('Impossible de créer un range'); }); static::updating(function (self $model) { self::assertLabelIsUnique($model->label, $model->id); $model->slug = Str::slug($model->label); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->picture_banner = FileStorageHelper::storeFile($model, $model->picture_banner, true); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); FileStorageHelper::removeOldFile($model, 'picture_banner'); }); static::deleting(function () { throw new \RuntimeException('Impossible de supprimer un range'); }); } // * METHODS /** * Asserts using validation that the label is unique. * * @param string $label * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If an actu label already exists. */ private static function assertLabelIsUnique(string $label, ?int $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } // * REDIRECTIONS. /** * All models that belongs to it. * * @return array */ protected function getRouteChildrenModels(): array { return ['App\Models\Subrange']; } /** * Get the front url of this model. * Should return the model.show route, with all binded elements if needed. * * @param string $modelKey * @return string */ public function getFrontUrl(string $modelKey): string { return route('fo.products.range-products', $modelKey); } // * RELATIONSHIPS /** * Subranges Has-Many relationship. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function subranges(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Subrange::class); } // * SEO /** * Transform to seoModel * * @return \Uwil\Seo\Laravel\Objects\SeoItem */ public function toSeoItem(): SeoItem { return new SeoItem( \trans($this->getTable()), $this->seo_title, $this->seo_description, true, \route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this), \route('fo.products.range-products', $this) ); } /** * toSitemapTag method * * @return Url|string|array */ public function toSitemapTag(): Url | string | array { $model = $this; $route = 'fo.products.range-products'; $params = ['range' => $model]; return SitemapHelper::generateUrl($route, $params, $model); } }