*/ protected $fillable = [ 'id', 'name', ]; /** * Define the custom columns (that won't be stored in the data JSON column) * by overriding the getCustomColumns() method. * * @see https://tenancyforlaravel.com/docs/v3/tenants * * @return array */ public static function getCustomColumns(): array { return [ 'id', 'name', ]; } /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { // * ASSERTIONS self::assertNameIsUnique($model->name); // * TRANSFORMATIONS $model->id = Str::slug($model->name); }); static::updating(function (self $model) { // * ASSERTIONS self::assertNameIsUnique($model->name, $model->id); // * TRANSFORMATIONS $model->id = Str::slug($model->name); }); } // * METHODS /** * Asserts using validation that the name is unique * * @param string|null $name * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If name already exists. */ private static function assertNameIsUnique(string|null $name, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); ToolboxHelper::assertFieldIsUnique($table, 'id', Str::slug($name), $id); } // * RELATIONSHIPS /** * The tenants that belong to the user. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class); } }