$products * Products Belongs-To-Many relationship. * @property-read \App\Models\Range $range Range Belongs-To relationship. */ class Subrange extends Model { use HasFactory; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'label', 'slug', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { // * EVENT CLOSURES. static::creating(function (self $model) { self::manageFields($model); }); static::updating(function (self $model) { self::manageFields($model); }); } // * METHODS /** * Common methods function. * * @param self $model * @return void */ private static function manageFields(self $model): void { self::assertLabelIsUnique($model->label, $model->id); $model->slug = Str::slug($model->label); } /** * Asserts using validation that the label is unique * * @param string $label * @param integer|null $id * @return void */ 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); } // * RELATIONSHIPS /** * Products Belongs-To-Many relationship. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function products(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Product::class); } /** * Range Belongs-To relationship. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function range(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Range::class); } }