Implementations of Json field unqiue validation rule and inspired by codezero-be/laravel-unique-translation
- PHP >= 7.0
- MySQL >= 5.7
- Laravel >= 5.5
Require the package via Composer:
composer require topview-digital/laravel-unique-json-rule
Laravel will automatically register the ServiceProvider.
For the following examples
Your form can also submit an array of contact.
<input name="contact[name]">
<input name="contact[email]">
<input name="contact[phone]">
We need to validate the entire array in this case.
$attributes = request()->validate([
'contact.name' => 'unique_json:clients,contact->name',
'contact.email' => UniqueJsonRule::for('clients','contact->email'),
]);
If you're updating a record, you may want to ignore the post itself from the unique check.
$attributes = request()->validate([
'contact.name' => 'unique_json:clients,contact->name,{$client->id}',
'contact.email' => UniqueJsonRule::for('clients','contact->email')->ignore($client->id),
]);
If your ID column has a different name, or you just want to use another column:
$attributes = request()->validate([
'contact.name' => 'unique_json:clients,contact->name,{$client->uuid},uuid',
'contact.email' => UniqueJsonRule::for('clients','contact->email')->ignore($client->uuid,'uuid'),
]);
You can pass your own error message with any of the following keys. The first one found will be used.
$attributes = request()->validate([
'contact.name' => 'unique_json:clients,contact->name,{$client->id}',
], [
'contact.name.unique_json' => 'Your custom :attribute error.',
]);
See a list of important changes in the changelog.
The MIT License (MIT). Please see License File for more information.