*/ 'nutritional_values' => 'array', 'status' => 'bool', 'cooking_mode' => CookingModeEnum::class, 'nutriscore' => NutriscoreEnum::class ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // * EVENT CLOSURES. static::creating(function (self $model) { // * ASSERTIONS static::assertFieldIsUnique('name', $model->name); ToolboxHelper::assertNoInternalBoUrls('description', $model->description); ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? ''); ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients); // * TRANSFORMATIONS $model->slug = Str::slug($model->name); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true); }); static::created(function (self $model) { static::flashStatus($model); }); static::updating(function (self $model) { // * ASSERTIONS static::assertFieldIsUnique('name', $model->name, $model->id); ToolboxHelper::assertNoInternalBoUrls('description', $model->description); ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? ''); ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients); // * TRANSFORMATIONS $model->slug = Str::slug($model->name); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); FileStorageHelper::removeOldFile($model, 'sticker'); static::flashStatus($model, true); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); FileStorageHelper::removeFile($model, 'sticker'); }); } // * Data structure /** * Get url input Template * * @return array */ public static function ingredient(): array { return [ /** * @var string Eg: Valeur énergétique. */ 'label' => '', /** * @var string Eg: 1076 kJ, 259 kcal. */ 'value' => '' ]; } // * 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); } // * RELATIONSHIPS /** * BelongsToMany certifications relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function certifications() { return $this->belongsToMany(Certification::class, 'product_certification'); } /** * BelongsTo certifications relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function range() { return $this->belongsTo(Range::class); } /** * BelongsToMany recipes relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function recipes() { return $this->belongsToMany(Recipe::class); } }