PictoEnum::class, ]; /** * The translatable columns. * * @var array */ public $translatable = [ 'seo_title', 'seo_description', 'label', 'slug', ]; // * EVENTS /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { self::manageFields($model); self::assertFieldIsUnique('label', $model->label); self::setOrder($model); }); static::updating(function (self $model) { self::manageFields($model); }); } // * METHODS /** * Set the slug. * * @param \Illuminate\Database\Eloquent\Model $game * * @return void */ private static function setSlug(Model $model) { $model->slug = Str::slug($model->label); } /** * Common methods function * * @param Model $model * @return void */ private static function manageFields(Model $model) { self::setSlug($model); } /** * Asserts using validation that the field is unique. * * @param string $nameField * @param mixed $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); } /** * Set order after the last element of the list. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ private static function setOrder(Model $model): void { $model->order = \intval(self::query()->max('order')) + 1; } // * RELATIONSHIPS /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function recipes() { return $this->hasMany('App\Models\Recipe'); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function products() { return $this->hasMany('App\Models\Product'); } }