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),
No description
22 Replies
Dennis Koch
Dennis Koch14mo ago
Sounds like you want unique() over multiple columns. I think you can pass them as an array. ->unique(['brand', 'model_id', 'name'])
wandiaprianto
wandiapriantoOP14mo ago
that's it,, thankyou @Dennis Koch
wandiaprianto
wandiapriantoOP14mo ago
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),
No description
Dennis Koch
Dennis Koch14mo ago
Is it an error or just your editor complaining? Maybe you need a comma separated string instead of an array
wandiaprianto
wandiapriantoOP14mo ago
i have tried, still same 😦
wandiaprianto
wandiapriantoOP14mo ago
No description
wandiaprianto
wandiapriantoOP14mo ago
No description
Dennis Koch
Dennis Koch14mo ago
That's the same as before. Not a string
wandiaprianto
wandiapriantoOP14mo ago
sorry, can you help me by using example of code?
Dennis Koch
Dennis Koch14mo ago
Array: ['brand', 'model_id'], String 'brand,model_id'
wandiaprianto
wandiapriantoOP14mo ago
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; }
wandiaprianto
wandiapriantoOP14mo ago
No description
Dennis Koch
Dennis Koch14mo ago
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
wandiaprianto
wandiapriantoOP14mo ago
sorry Sir, like this? Forms\Components\TextInput::make('name') ->required() ->unique('car_brand_id, car_model_id, name', ignoreRecord: true) ->columnSpan(2),
Dennis Koch
Dennis Koch14mo ago
Yes
wandiaprianto
wandiapriantoOP14mo ago
cannot, still get an error
Dennis Koch
Dennis Koch14mo ago
What error?!
wandiaprianto
wandiapriantoOP14mo ago
i got this error
No description
wandiaprianto
wandiapriantoOP14mo ago
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(); });
joaoppcastro
joaoppcastro12mo ago
Can you be more specific? I have the same error in the same area...
No description
joaoppcastro
joaoppcastro12mo ago
Forms\Components\TextInput::make('slug') ->hint('Translatable') ->hintColor('primary') ->hintIcon('heroicon-m-language') ->disabledOn('edit') ->required() ->unique(Sitemap::class, 'slug', ignoreRecord:true) ->maxLength(255),
Want results from more Discord servers?
Add your server