© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
3 replies
Daniel Plomp

Unique() validation with multiple columns

I have a polymorphic relationship between e.g. a
Client
Client
and
Addresses
Addresses
. I want to be able to add multiple addresses for a
Client
Client
. There is however a unique constraint on the table that looks like this:

$table->unique(['address_type_id', 'addressable_id', 'addressable_type'], 'unique_addressable');
$table->unique(['address_type_id', 'addressable_id', 'addressable_type'], 'unique_addressable');


This is doing exactly what it should do, but it causes some troubles in the Filament Panel.
If I add an address the first time, it is no problem.

If I want to add a new address with the same
address_type_id
address_type_id
I get this error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry


Which is also something I would expect. Only it returns in a 500 error. What I want is to get a nice validation error somewhere on my form, presumably with the AddressType Select field.
I tried to use this rule:

->unique(ignoreRecord: true)
->unique(ignoreRecord: true)


Then I get a nice validation message below my Select field, but it also triggers when I just update an existing record.
Next, I tried this rule:

->unique(ignoreRecord: true, modifyRuleUsing: function (Unique $rule, $state) {
    return $rule
        ->where('addressable_type', Address::class)
        ->where('address_type_id', $state);
})
->unique(ignoreRecord: true, modifyRuleUsing: function (Unique $rule, $state) {
    return $rule
        ->where('addressable_type', Address::class)
        ->where('address_type_id', $state);
})


This allows me to update and save my existing record, but still when trying to add a new record with the same Address Type, it gives me this error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry


So, I want to know how I can the right validation message inside my form, instead of a 500 error. Not sure if this could work because with new records you only know when inserting them into the database if the fields are unique.
How would this work in Filament?
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Unique Multiple Columns
FilamentFFilament / ❓┊help
2y ago
unique multiple columns
FilamentFFilament / ❓┊help
3y ago
unique multiple columns
FilamentFFilament / ❓┊help
3y ago
Unique validation on 2 or more columns
FilamentFFilament / ❓┊help
12mo ago