Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Nano Id Laravel Package

snortlin/nano-id

Laravel-friendly NanoID generator for PHP: create short, URL-safe, collision-resistant IDs with configurable length and alphabet. Simple API, lightweight and fast—ideal for public identifiers, tokens, and model keys without exposing sequential IDs.

View on GitHub
Deep Wiki
Context7

A tiny, secure, URL-friendly, unique string ID generator for PHP

Frequently asked questions about Nano Id
How do I install snortlin/nano-id in a Laravel project?
Run `composer require snortlin/nano-id` in your project directory. The package integrates seamlessly with Laravel’s service container and requires no additional configuration for basic usage. For advanced setups, register the service provider in `config/app.php` if needed.
Can I use NanoIDs as primary keys in Laravel Eloquent models?
Yes, but you’ll need to configure your model to use a custom primary key. Set `$primaryKey = 'nano_id'` and `$keyType = 'string'` in your model, then generate IDs via `NanoId::generate()`. Ensure your database column is a `CHAR` or `VARCHAR` type with a UNIQUE constraint to handle collisions.
What’s the collision risk with snortlin/nano-id, and how do I mitigate it?
Collision risk is extremely low with default settings (e.g., 21-character IDs have a ~1 in 2^132 chance of collision). For high-volume systems, use longer IDs or implement retry logic in your application. Database UNIQUE constraints will automatically handle duplicates if they occur.
Does snortlin/nano-id work with Laravel 8, 9, or 10?
The package is compatible with Laravel 8+ and follows modern PHP standards. It has no strict version dependencies beyond PHP 8.0+, so it should work out of the box with Laravel’s latest releases. Check the package’s `composer.json` for exact PHP version requirements.
How do I customize the NanoID alphabet or length?
Use the `generate()` method with parameters: `NanoId::generate(10, 'abcdefghijklmnopqrstuvwxyz0123456789')` for a 10-character ID with a custom alphabet. The default alphabet is URL-safe and includes uppercase letters, lowercase letters, and numbers. Avoid special characters unless explicitly needed.
Can I use NanoIDs for API tokens or URL slugs in Laravel?
Absolutely. NanoIDs are ideal for tokens and slugs due to their URL-friendly format and brevity. Replace `Str::random(32)` with `NanoId::generate(10)` for shorter, human-readable tokens or slugs. Ensure your routes or API endpoints handle the string format correctly.
What’s the performance impact of generating NanoIDs in a loop?
NanoID generation is highly performant (O(1) complexity) and adds minimal overhead. However, generating thousands of IDs in a tight loop may introduce slight latency. For bulk operations, consider batching or pre-generating IDs outside critical paths.
How do I test NanoID uniqueness in my Laravel application?
Write unit tests to verify ID generation by checking for collisions in a controlled environment. Use PHPUnit to generate thousands of IDs and assert uniqueness. For database integration, test UNIQUE constraint handling by simulating duplicate inserts and verifying error responses.
Are there alternatives to snortlin/nano-id for Laravel?
Yes, alternatives include `ramsey/uuid` for UUIDs (longer but globally unique) or `spatie/laravel-activitylog` for event-based IDs. For NanoID-style IDs, `web-token/jwt-framework` or custom implementations using `random_bytes()` are options, but `snortlin/nano-id` offers the best balance of simplicity and Laravel integration.
How do I migrate from auto-increment IDs to NanoIDs in an existing Laravel app?
Start by adding a new `nano_id` column to your database table, then backfill existing records with generated NanoIDs. Update your model to use the new primary key and ensure all queries, relationships, and ORM features (e.g., `find()`) work with the string-based ID. Test thoroughly in staging before full deployment.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon