unique multiple columns
please help me, how can i set unique with condition
data example:
Toyota Avanza G V
Toyota Alphard G X (but can't create, return message The Name Has Already been taken)
Toyota Avanza G X
this is my code:
Forms\Components\TextInput::make('name')
->required()
->unique(Type::class, 'name')
->columnSpan(2),
22 Replies
Sounds like you want
unique()
over multiple columns. I think you can pass them as an array. ->unique(['brand', 'model_id', 'name'])
that's it,, thankyou @Dennis Koch
sorry, why i get an error?
here is my code:
Forms\Components\TextInput::make('name')
->required()
// ->unique(Type::class, 'name')
->unique(['car_model_id', 'name'])
->columnSpan(2),
Is it an error or just your editor complaining? Maybe you need a comma separated string instead of an array
i have tried, still same 😦
That's the same as before. Not a string
sorry, can you help me by using example of code?
Array:
['brand', 'model_id']
,
String 'brand,model_id'
i add this, but not work
protected function mutateFormDataBeforeCreate(array $data): array
{
$not_unique = Type::where([
['car_brand_id', '=', $data['car_brand_id']],
['car_model_id', '=', $data['car_model_id']],
['name', '=', $data['name']]
])->exists();
if ($not_unique) {
Notification::make()
->title('combination is not unique')
->danger()
->send();
throw ValidationException::withMessages(['brand, model and name combination is not unique']);
}
return $data;
}
Didn't I just show you the difference? Why are you still using the array?
Maybe you need a comma separated string instead of an array
sorry Sir, like this?
Forms\Components\TextInput::make('name')
->required()
->unique('car_brand_id, car_model_id, name', ignoreRecord: true)
->columnSpan(2),
Yes
cannot, still get an error
What error?!
i got this error
this is my code:
return $form
->schema([
Forms\Components\Select::make('car_brand_id')
->live()
->relationship('brand', 'name')
->searchable()
->options(Brand::limit(10)->pluck('name', 'id'))
->required()
->afterStateUpdated(fn ($set) => $set('car_model_id', null)),
Forms\Components\Select::make('car_model_id')
->searchable()
->live()
->required()
->options(function (callable $get) {
if (!$get('car_brand_id')) {
return;
} else {
$brand = Brand::find($get('car_brand_id'));
if (!$brand) {
return CarModel::limit(10)->pluck('name', 'id');
}
return $brand->models->pluck('name', 'id');
}
}),
Forms\Components\TextInput::make('name')
->required()
->unique('car_brand_id, car_model_id, name', ignoreRecord: true)
->columnSpan(2),
])->columns(2);
this is my database table
Schema::create('car_types', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('car_brand_id');
$table->foreign('car_brand_id')->references('id')->on('car_brands');
$table->unsignedBigInteger('car_model_id');
$table->foreign('car_model_id')->references('id')->on('car_models');
$table->string('name');
$table->timestamps();
});
thankyou, i solved by use reference on this link https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create
Can you be more specific? I have the same error in the same area...
Forms\Components\TextInput::make('slug')
->hint('Translatable')
->hintColor('primary')
->hintIcon('heroicon-m-language')
->disabledOn('edit')
->required()
->unique(Sitemap::class, 'slug', ignoreRecord:true)
->maxLength(255),