topview-digital / laravel-unique-json-rule
Installs: 5 199
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: >=7.0.0
- laravel/framework: ^5.5.0
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: 0.9.*
- orchestra/testbench: ~3.5.0
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2024-10-19 14:23:03 UTC
README
Laravel Unique Json
Check if a record value in a JSON column is unique in the database.
Implementations of Json field unqiue validation rule and inspired by codezero-be/laravel-unique-translation
Requirements
- PHP >= 7.0
- MySQL >= 5.7
- Laravel >= 5.5
Installation
Require the package via Composer:
composer require topview-digital/laravel-unique-json-rule
Laravel will automatically register the ServiceProvider.
Usage
For the following examples
Validate an Array of Contacts
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'), ]);
Ignore a Record with ID
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), ]);
Ignore Records with a Specific Column and Value
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'), ]);
Error Messages
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.', ]);
Changelog
See a list of important changes in the changelog.
License
The MIT License (MIT). Please see License File for more information.