topview-digital/laravel-unique-json-rule

1.0 2019-03-14 02:34 UTC

This package is auto-updated.

Last update: 2024-10-19 14:23:03 UTC


README

GitHub release Scrutinizer Code Quality Build Status Code Intelligence Status License Total Downloads HitCount

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.