Unique validation rule not applicable to db index

in my migration file i have applied a unique constraint to the email column of my user model. when trying to addionally apply a frontend validation in the user resource using the unique function, i get no result. thus i am still able to insert duplicat emails resulting in an sql error.
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
TextInput::make('email')
->required()
->unique(table: User::class, column: 'email')
TextInput::make('email')
->required()
->unique(table: User::class, column: 'email')
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user@user.com' for key 'users.users_email_unique'
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user@user.com' for key 'users.users_email_unique'
12 Replies
N@meless
N@meless4mo ago
TextInput::make('email') ->required() ->unique(User::class, 'email', ignoreRecord: true) or TextInput::make('email') ->required() ->rule(\Illuminate\Validation\Rule::unique('users', 'email')->ignore($record->id))
Charles
CharlesOP4mo ago
both suggested solutions have no effect. there is a syntax error in the second one
Nben M.
Nben M.4mo ago
It might work, however make sure to apply the corresponding field i.e email instead of slug
No description
N@meless
N@meless4mo ago
Because of your version TextInput::make('email') ->required() ->unique( User::class, // Model class 'email', // Column ignoreRecord: true // <-- this tells Filament to automatically ignore current record ID );
Charles
CharlesOP4mo ago
neither of the suggested solutions have any effect. since this error also occurs when creating new records, i don't think the ignoreRecord affects the described situation
Groli
Groli4mo ago
Maybe try creating your own rule like this:
TextInput::make('email')
->required()
->unique(modifyRuleUsing: function (Unique $rule) {
// your own validation...
})
TextInput::make('email')
->required()
->unique(modifyRuleUsing: function (Unique $rule) {
// your own validation...
})
If that doesn't work, maybe try checking elsewhere for mistakes...
Dennis Koch
Dennis Koch4mo ago
Is this the standard panel page or a custom page?
Charles
CharlesOP4mo ago
Standard panel, particularly in a resource i am trying to fix this issue for several weeks now. unfortunately i can't share the repository due to a nda
Hasan Tahseen
Hasan Tahseen4mo ago
So is the record in db soft deleted? maybe you have some condition that respect the soft deleted record which may pass the unqiue validation and goes to db
Dennis Koch
Dennis Koch4mo ago
Maybe use Debugbar or similar to check the actual query
Hasan Tahseen
Hasan Tahseen4mo ago
yeah that also.
Charles
CharlesOP4mo ago
this could be the error, i will investigate this the resource/model has enabled soft deletes, is there a way to handle unique validation for soft deletable records

Did you find this page helpful?