justbetter/laravel-unique-values

Package to generate unique values with support for concurrency utilizing cache locking
7,346 53
Install
composer require justbetter/laravel-unique-values
Latest Version:1.1.0
PHP:^8.3
Last Updated:Apr 2, 2026
Links: GitHub  ·  Packagist
Maintainer: justbetter

Package to generate unique values

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.

Related Packages

mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

21,010,090 1,982
kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6,282,182 647
mews/captcha

Laravel 5/6/7/8/9/10/11/12 Captcha Package

6,095,827 2,586
ajthinking/archetype

Programmatically edit PHP and Laravel files.

4,372,487 272