$nutritional_values The nutritional values as a value-label list. * @property boolean $status The publication status. * @property integer $range_id The range associated. * * @package App\Models */ class Range extends Model implements Sitemapable { use HasFactory; use HasFlashStatus; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'slug', 'subtitle', 'seo_title', 'seo_description', 'picture', 'picture_alt', 'picture_title', 'banner', 'banner_alt', 'banner_title', 'sticker', 'sticker_alt', 'sticker_title', 'status' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function () { throw new \RuntimeException('Impossible de créer une gamme'); }); static::updating(function (self $model) { // * ASSERTIONS self::assertFieldIsUnique('name', $model->name, $model->id); self::assertAtLeastOneIsPublished($model); // * TRANSFORMATIONS $model->slug = Str::slug($model->name); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->banner = FileStorageHelper::storeFile($model, $model->banner, true); $model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); FileStorageHelper::removeOldFile($model, 'banner'); FileStorageHelper::removeOldFile($model, 'sticker'); static::flashStatus($model, true); }); static::deleting(function () { throw new \RuntimeException('Impossible de supprimer une gamme'); }); } // * METHODS /** * Asserts using validation that the field is unique. * * @param string $nameField * @param string $valueField * @param integer|null $id * @return void */ private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id); } /** * Asserts using validation that the name is unique * * @param self $model * @return void * @throws \Illuminate\Validation\ValidationException If no model type is published. */ private static function assertAtLeastOneIsPublished(self $model) { if (!$model->status and self::query()->where('status', true)->count() < 2) { throw ValidationException::withMessages([ 'error' => trans('Au moins un(e) :model doit être publié, impossible de dépublier', [ 'model' => trans('models.classes.' . $model->getTable()) ]) ]); } } /** * toSitemapTag method * * @return Url|string|array */ public function toSitemapTag(): Url | string | array { $model = $this->loadMissing('products'); $route = 'ranges.products'; $params = ['publishedRange' => $model->slug, 'publishedProduct' => $model]; return SitemapHelper::generateUrl($route, $params, $model); } // * RELATIONSHIPS /** * BelongsToMany certifications relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function products() { return $this->hasMany(Product::class); } }