Ben Borgers

How to fix unique() for editing in Laravel Filament

May 18, 2022

I created an admin panel using Laravel Filament and used the unique() modifier on an input, but I was unable to save subsequent changes after creation because the resource wasn’t unique to itself (another resource with the same value already existed, which was itself).

Filament’s unique() method comes with a way of ignoring certain models, which we can use to ignore itself.

That way, we’ll be able to save changes to a model without getting an error saying that a field is not unique.

Update your usage of the unique() modifier to be like this:

-    ->unique();
+    ->unique(ignorable: fn ($record) => $record);

Now, the currently-being-edited model will be ignored when checking for uniqueness.

Subscribe to my newsletter!

A weekly round-up of new blog posts and updates to projects I’m working on.