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

Laravel Unique Values

Laravel Unique Values stats

Downloads
0
Stars
4
Open Issues
0
Forks
0

View on GitHub →

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

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 photo

We're an innovative development agency from The Netherlands building awesome websites, webshops and web applications with Laravel and Magento.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Justbetter Laravel Unique Values Related Articles

Generate Persistent Unique Values in Laravel image

Generate Persistent Unique Values in Laravel

Read article
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu