Customizing the Unique validation rule

How would I appropriately customize the Unique validation rule/method that Filament has according to the following? Here is my categories table:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained()->cascadeOnDelete();
$table->string('name')->index();
$table->string('type');
$table->string('color');
$table->boolean('enabled')->default(true);
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();

$table->unique(['company_id', 'name', 'type']);
});
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained()->cascadeOnDelete();
$table->string('name')->index();
$table->string('type');
$table->string('color');
$table->boolean('enabled')->default(true);
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();

$table->unique(['company_id', 'name', 'type']);
});
This is what I have so far for the Field:
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Request $request) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $request->input('type'));
}),
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Request $request) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $request->input('type'));
}),
Will the above work? Or do I need to do something like this or something else?
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Forms\Get $get) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $get('type'));
}),
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Forms\Get $get) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $get('type'));
}),
5 Replies
ZedoX
ZedoX15mo ago
You can't use $request in livewire requests. Second approach is the way to go
cheesegrits
cheesegrits15mo ago
Yes, you can inject $request, but it's only going to work on page load (for example in the mount() method of a page), not for Livewire method calls. Livewire doesn't use HTTP requests, you're not doing a POST or a GET or whatever.
awcodes
awcodes15mo ago
And since it’s ajax based the request isn’t actually the url in the browser.
Andrew Wallo
Andrew WalloOP15mo ago
Oh okay
Want results from more Discord servers?
Add your server