*/ protected $fillable = [ 'label', 'slug', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::updating(function (self $model) { self::assertLabelIsUnique($model->label, $model->id); $model->slug = Str::slug($model->label); }); } // * 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 label already exists. */ private static function assertLabelIsUnique($label, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } // * RELATIONSHIPS /** * BelongsToMany news * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function news() { return $this->belongsToMany(News::class); } }