A simple package to generate Nano IDs in Laravel.
A Nano ID is a URL-friendly, unique string ID.
It is similar to UUIDs, but shorter and more readable.
Nano IDs are 21 characters long by default and can be customized to be longer or shorter.
- Read more on Medium: Nano ID: A Tiny, Secure URL-friendly Unique String ID Generator
- Or on GitHub: Nano ID
- A facade to generate Nano IDs
- A helper function to use as alternative to the facade
- Configuration options to customize the defaults for your Nano IDs
- Prefix
- Suffix
- Alphabet
- Size
- Random string generator
- More to come
- Easily extensible with custom validation rules (like uniqueness or swear word checks)
- PHP >= 8.0
composer require fanmade/laravel-nanoid
use Fanmade\NanoId\Facades\NanoID;
echo NanoID::generate(); // Returns a Nano ID
echo NanoID::generate(length: 10); // Returns a Nano ID with a length of 10
echo NanoID::generate(length: 10, prefix: 'prefix_'); // Returns a Nano ID with a length of 10 and a prefix of 'prefix_'
echo NanoID::generate(suffix: '_suffix'); // Returns a Nano ID with a suffix of '_suffix'
echo NanoID::generate(alphabet: '0123456789'); // Returns a Nano ID only containing numbers
echo nano_id(); // The helper function accepts the same parameters as the generate method
Publish the configuration file
php artisan vendor:publish --tag=nanoid-config
Option | Description | Default |
---|---|---|
prefix |
An optional prefix which will be added to the Nano ID | '' |
suffix |
An optional suffix which will be added to the Nano ID | '' |
alphabet |
The alphabet to use for generating the Nano ID | 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_ +abcdefghijklmnopqrstuvwxyz- |
size |
The default size/length of the Nano ID | 21 |
generator |
The generator class to use for generating random strings | \Fanmade\NanoId\Generator\HidehaloStringGenerator |
include_prefix_in_length |
Controls if the prefix is included or excluded in the size limitation | true |
include_suffix_in_length |
Controls if the suffix is included or excluded in the size limitation | true |
vendor/bin/pest
or
composer test
- hidehalo/nanoid-php - The original Nano ID package for PHP. This is also used in the background of this package.
- yondifon/laravel-nanoid - More focused on adding the Nano IDs to the models.
- ttbooking/laravel-nanoid - This one has more focus on extending the string helpers.