Package

Justbetter Laravel Unique Values

Package to generate unique values with support for concurrency utilizing cache locking

Package

justbetter/laravel-unique-values

View on GitHub →
Laravel Unique Values Banner

Package to generate unique values

Tests Coverage Analysis Total downloads

This package generates persistent unique values with support for concurrency utilizing scoped cache locking. It stores generated unique values in the database.

Generated values are unique per scope and can be generated using a callback method. This package only supports strings.

Features

  • Generate unique values
  • Support for concurrency
  • Customizable generator
  • Maximum attempts
  • Support for subjects

Installation

You can install the package via composer:

composer require justbetter/laravel-unique-values

Usage

use JustBetter\UniqueValues\Support\UniqueValue;

$uniqueValue = UniqueValue::make()
    ->scope('unique-scope')
    ->generator(function (int $attempt): string {
        return match ($attempt) {
            0 => 'unique-value',
            default => 'unique-value-'.$attempt,
        };
    })->generate();

Output after first run: unique-value. Output after second run: unique-value-1. Output after third run: unique-value-2.

Maximum attempts

You can configure the maximum attempts, this is three by default. If the maximum attempts are reached an exception is thrown.

use JustBetter\UniqueValues\Support\UniqueValue;

$uniqueValue = UniqueValue::make()
    ->scope('unique-scope')
    ->attempts(2)
    ->generator(function (int $attempt): string {
        return match ($attempt) {
            0 => 'unique-value',
            default => 'unique-value-'.$attempt,
        };
    })->generate();

Subject

You can add a subject to retrieve a previously generated value for the subject. Optionally you can add an override flag to run the generation again for the subject.

use JustBetter\UniqueValues\Support\UniqueValue;

$uniqueValue = UniqueValue::make()
    ->scope('unique-scope')
    ->subject('subject')
    ->generator(function (int $attempt): string {
        return match ($attempt) {
            0 => 'unique-value',
            default => 'unique-value-'.$attempt,
        };
    })->generate();

First run will output: unique-value. Second run will output: unique-value. Only one unique value will be stored in the database.

Quality

To ensure the quality of this package, run the following command:

composer quality

This will execute three tasks:

  1. Makes sure all tests are passed
  2. Checks for any issues using static code analysis
  3. Checks if the code is correctly formatted

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Unique Values footer

Justbetter Laravel Unique Values Related Articles

Generate Persistent Unique Values in Laravel image

Generate Persistent Unique Values in Laravel

Read article
Generate a Unique Series of Values with Earmark image

Generate a Unique Series of Values with Earmark

Read article
Laravel Unique image

Laravel Unique

Read article
Improvements to the Laravel unique and exists validation rules image

Improvements to the Laravel unique and exists validation rules

Read article
Handling Default Values in Laravel Request using mergeIfMissing image

Handling Default Values in Laravel Request using mergeIfMissing

Read article
Humanize String, Number, and Date Values into Readable Formats  image

Humanize String, Number, and Date Values into Readable Formats

Read article

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum