Forms
Similar
A yellow warning that lists records similar to the one about to be created, with buttons to create it anyway or cancel, and optionally one to use an existing record instead. Use it in create forms where duplicates with slightly different names are a problem, such as categories, tags or customers. The TALLKit\Livewire\ConfirmsSimilar trait runs the save → check → confirm flow on the Livewire side.
Nothing to show
#[Similar] marks the field being checked. Submitting calls saveSimilar, which collapses repeated spaces in that field, validates and runs findSimilar, here with the built-in querySimilar. When nothing comes back, storeSimilar runs right away. Otherwise $similar gets the records and the form shows a box titled "Similar records found" that lists them and asks Do you want to create "Food" anyway?, with Create anyway and Cancel buttons. :submit="! $similar" hides the form's submit button while the box is open. Create anyway validates again and calls storeSimilar. Cancel closes the box. Typing in the field also closes it and clears its error, so the list never refers to a value the user has since changed; if the value changes in the same request as the click on Create anyway (before the debounce fires, or with a deferred wire:model), the new value is checked again instead of stored. Inside a Livewire component tk:similar reads $similar and the #[Similar] field by itself. Every default text is translated.
Using an existing record #
Each listed record becomes a button, its name on the left and "Use this ›" on the right. Clicking the Foods row calls useExisting(1) with that record's id.
Similar records found
Nothing to show
Linking to the record #
href takes a closure that gets each item and returns its URL. Each row becomes a link ending in "Open ›", to /categories/4 for Calçados, that navigates with wire:navigate and opens in a new tab with Ctrl/Cmd+click. With both href and select, href wins. select:target="_blank" opens every link in a new tab.
Nothing to show
The same name #
Typing "calcados" when "Calçados" exists lists "Calçados" first, with a "Same name" badge, under the title "This record already exists" and the text Use the existing record or change the name.. Create anyway is not shown, and confirmSimilar does nothing while the list has such a record, so the duplicate can't be created from the browser either. The user picks the record with select, opens it with href, or changes the name, which closes the box.
querySimilar adds 'same' => true to a record whose slug equals the field's, so names that differ only in case, accents or spacing count as the same. A findSimilar of your own marks the item the same way. Leave the unique rule out of rules() for this: it fails before findSimilar runs, so the user gets the validation error instead of the record. A UniqueConstraintViolationException from storeSimilar, when two users save the same name at once, still becomes the field's error.
Finding similar records #
querySimilar compares the slug of the #[Similar] field with the slug of the column in each record of the query: a record is similar when one slug contains the other ("camisa" and "Camisa polo"), or when similar_text gives at least threshold percent ("Camisetas" and "Camisas"). Slugs ignore case and accents, so "calcados" and "Calçados" count as the same. Containment only counts when both slugs have at least minLength characters (3 by default), so "iv" is not similar to "Atividades"; pass minLength: 2 to accept two-letter matches. A value without letters or digits finds nothing. The column defaults to the #[Similar] field's name, and only the key and that column are loaded, so nothing else of the record reaches the page. Every record of the query is compared in PHP: narrow the query first on large tables. The whereKeyNot above leaves out a record by hand; #[Similar(record: ...)] does it for you, see the next example.
Nothing to show
Editing a record #
record names the property holding the model being edited. querySimilar leaves that record out of the list, the question becomes Do you want to save "Camisa" anyway? and the button Save anyway. Saving without changing the name stores right away, without looking for similar records. When the property holds a model that is not saved yet (new Category), everything works as when creating, so one component can do both.
Nothing to show
Other fields as key and label #
item-key and item-label choose which field identifies each record and which one is shown, read with data_get, so the items can be arrays or models. Here each row shows the email, and select would receive the uuid.
Nothing to show
Without the trait #
The component only renders: confirm and cancel take the names of any Livewire actions, and the texts can all be replaced. With no items it renders nothing.
This customer may already exist
- Ana Souza
- Ana Souza Lima
Nothing to show
Customization #
Other attributes go to the alert (type, variant, border, size); items: (the panel), item:, select: (each row button), actions:, confirm: and cancel: reach the parts.
Similar records found
Nothing to show
Reference
Similar
| Prop | Type | Default | Description |
|---|---|---|---|
items |
array | Collection | the component's $similar |
The similar records. When empty, nothing is rendered. |
value |
string | the #[Similar] field |
The value being created, quoted in the default question. |
item-key |
string | id |
The field of each item used for its wire:key and passed to select. |
item-label |
string | name |
The field of each item shown in the list. |
href |
Closure | null |
Returns the URL of each item, e.g. fn ($item) => route('categories.show', $item['id']). Turns each item into a link that ends in "Open ›" and wins over select. |
select |
string | null |
A Livewire action called with the item key, e.g. useExisting → useExisting(1). Turns each item into a button that ends in "Use this ›"; without it the items are plain text. |
confirm |
string | confirmSimilar |
The Livewire action of the Create anyway button, labeled Save anyway when editing a record. |
cancel |
string | cancelSimilar |
The Livewire action of the Cancel button. |
message |
string | Do you want to create ":value" anyway? |
The question under the list, translated. When editing a record the default is Do you want to save ":value" anyway?, without a value it is Do you want to continue anyway?, and when an item has the same name it is Use the existing record or change the name.. |
title |
string | Similar records found |
The heading of the box, translated. When an item has the same name the default is This record already exists. |
size |
string | null |
The size of the box and of the Create anyway and Cancel buttons. |
Other attributes go to the alert the box is built on, so icon, variant, border and closable work as there.
Prefixes
| Prefix | Goes to |
|---|---|
items: |
The list. |
item: |
Each list item. |
select: |
Each item's button or link, when select or href is set. select:label replaces "Use this" ("Open" with href). |
actions: |
The row with the Create anyway and Cancel buttons. |
confirm: |
The Create anyway button. |
cancel: |
The Cancel button. |
ConfirmsSimilar trait
| Member | Description |
|---|---|
#[Similar(record: ?string)] |
Attribute for the field being checked. Editing it clears $similar and the field's error. record names the property with the model being edited. |
public array $similar |
The records found by the last saveSimilar. Locked: the browser cannot change it. |
saveSimilar() |
Runs Str::squish on the #[Similar] field, validates, runs findSimilar() and calls storeSimilar() when nothing is similar. When the field still equals the record's saved value, it skips findSimilar(). |
confirmSimilar() |
Validates again, clears $similar and calls storeSimilar(). When $similar is already empty because the #[Similar] field changed, it runs saveSimilar() instead. Does nothing while an item has same. |
cancelSimilar() |
Clears $similar. |
findSimilar(): iterable |
Abstract: return the records similar to the current input. |
querySimilar($query, $column, $threshold, $minLength) |
Returns the records of an Eloquent query (or relation) similar to the #[Similar] field, as [key, column] arrays, leaving out the record being edited; the one with the same slug as the field also has 'same' => true. $column defaults to the field name, $threshold to 75, $minLength (the shortest slug that matches by containment) to 3. |
storeSimilar() |
Abstract: create the record. What it returns (a redirect, for instance) is returned by saveSimilar and confirmSimilar. A UniqueConstraintViolationException thrown here becomes the validation.unique error on the #[Similar] field; without the attribute it is thrown again. |
Validation runs only when the component has rules (rules(), $rules or #[Validate]).
Customization
Classes and attributes on the tag go to the box. To reach an inner element, prefix the attribute (listed in the Reference above).
Nothing to show
Accessibility
The box is a warning alert: it has role="alert" and aria-live="assertive", so a screen reader announces the list as soon as it appears. All actions are regular buttons, reached with Tab. With select, each item's button is named by the record and its label ("Foods Use this"), so the buttons can be told apart.
Behavior notes
titleandmessagetake plain text only; a slot with that name is not used.- Item labels are printed as they are, never passed through the translator, so a record named "Cancel" is not shown as "Cancelar".
- Only the
#[Similar]field closes the box when it changes; other fields of the form leave it open. Without the attribute nothing closes it but Cancel and Create anyway. - The unchanged check compares the field with the attribute of the same name on the record (
getOriginal), not withquerySimilar'scolumn, and only exact strings count: changing only the case still looks for similar records. itemsandvaluepassed as props win over the ones read from the component, so a secondtk:similarwith its own data can sit in the same component.- A record with the same name blocks creating: only
select(orhref) and Cancel are offered, andconfirmSimilarrefuses. Items withoutsame, such as the ones afindSimilarof your own returns, never block.