@oscaribarra25 Check for Illuminate\Validation\Rule;
Here: https://laravel.com/docs/10.x/validation#rule-unique below you can find this section
Forcing a Unique Rule to Ignore a Given ID:
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I am a bit lost trying to implement a unique rule on a related model inside a Livewire component.
I have this livewire component called CreateEditContact with a form for contact fields plus two dynamic tables one for contact emails and the other for contact phones. These two tables have the options to add and remove dynamically in the form. This livewire component also have the following rules
'contact_emails.*.email_address' => 'required|email|max:50|unique:contact_emails,email_address'
'contact_phones.*.phone_number' => 'required|string|max:12|unique:contact_phones,phone_number'
Both of them work fine when creating a new contact but when I am trying to edit the unique validation is telling the email and phone already exists because it is already in the database for the model that is being edited. I know the unique rule has a third parameter to set the id of the record to be ignored. But not sure how to get this id for a related model inside the rule definition.
The database tables are according to this migrations (I removed the extra fields from this to shorten this post)
Contacts table
Schema::create('contacts', function (Blueprint $table) {
$table->id();
});
Contact emails table
Schema::create('contact_emails', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('contact_id');
$table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade')->onUpdate('cascade');
$table->string('email_address');
});
Contact phones table
Schema::create('contact_phones', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('contact_id');
$table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade')->onUpdate('cascade');
$table->string('phone_number');
});
Can anyone tell me how can I fix this validation rule to successfully ignore the current record if editing.
Thanks in advance.
Best regards
@oscaribarra25 You're on a right track. To get the id of the relation, you're supposed to create a relation first in the model.
Let say in contacts model define HasMany relation
public function contactEmails(): HasMany
{
return $this->hasMany(ContactEmails::class, 'contact_id');
}
and/or vice-versa for ContactEmails with HasOne
public function contact(): HasOne
{
return $this->hasOne(Contact::class, 'id', 'contact_id');
}
But I'm not sure you can pass a relation like this, $this->contact_emails[???]->id, because it is a HasMany relationship. l'd rather make a custom validation where check if the current email exists in other contacts.
https://laravel.com/docs/10.x/validation#using-rule-objects
protected array $data = [];
public function setData(array $data): static
{
$this->data = $data;
return $this;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!is_null($this->data['contactId'])) {
$existingEmail = ContactEmail::where('email', $value)->where(
'contact_id',
'!=',
$this->data['contactId']
)->exists();
if ($existingEmail) {
$fail('The email already belongs to another contact.');
}
}
}
After, in the validation part call it something like this: 'email' => new EmailUniqueness()
Please or to participate in this conversation.