Download the PHP package awaresoftware/custom-id without Composer
On this page you can find all versions of the php package awaresoftware/custom-id. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package custom-id
Custom ID Generator for Laravel
A Laravel package for generating unique custom IDs with configurable character sets, lengths, and prefixes. Perfect for creating human-readable, collision-resistant identifiers for your Eloquent models.
Features
- 🎲 Configurable ID generation - Set length, prefix, and character set per model
- 🔒 Collision detection - Automatic retry mechanism with configurable attempts
- 🗑️ Soft-delete aware - Prevents ID reuse from soft-deleted records
- 🚀 Race condition handling - Retries on unique constraint violations from concurrent inserts
- 🎯 Simple API - Just use a trait and implement one method
- 📝 Custom exceptions - Detailed error information for debugging
- ⚡ Zero dependencies - Only requires
illuminate/support
Installation
Quick Start
1. Use the Trait
Add the HasCustomId trait to your model:
2. Update Your Migration
Set the primary key as a string:
3. Create Records
IDs are generated automatically:
Configuration
Publish the config file:
Or publish everything at once (config + migration):
Edit config/custom-id.php:
Per-Model Configuration
Override getCustomIdConfig() in your model:
Or load from your app's config:
Converting Users Table to Custom IDs
The package includes an optional migration to convert your existing users table from auto-incrementing integer IDs to custom string IDs.
Publish the Migration
This will create a timestamped migration in your database/migrations directory.
Configure the Migration
Before running the migration, update your config/custom-id.php:
Auto-Detected Tables
The migration automatically handles these common Laravel tables:
sessions(user_id)personal_access_tokens(tokenable_id - polymorphic)notifications(notifiable_id - polymorphic)oauth_access_tokens(user_id)oauth_auth_codes(user_id)oauth_clients(user_id)
Run the Migration
Update Your User Model
Add the HasCustomId trait to your User model:
Important Notes
- Backup your database before running this migration
- Test thoroughly in a development environment first
- The migration supports MySQL, PostgreSQL, and SQLite
- Reverting the migration assigns new sequential integer IDs (original IDs cannot be restored)
- Related tables configured in the config will have their columns converted to string type
Advanced Usage
Soft Delete Awareness
The package automatically detects if your model uses SoftDeletes and includes trashed records when checking for ID uniqueness:
Race Condition Handling
Two-phase approach separates collision detection from race condition handling:
- Collision detection — The service checks existing records (including soft-deleted) before assigning an ID
- Concurrent inserts —
performInsert()catchesUniqueConstraintViolationExceptionand regenerates the ID, retrying up to 3 times
Custom Exception Handling
Generation failures — after exhausting all retry attempts:
Configuration errors — invalid config values throw InvalidArgumentException:
Using the Service Directly
Using the Facade
Character Sets
Default Character Set
Excludes: 0, O, 1, I, L (ambiguous characters)
Common Alternatives
Alphanumeric (uppercase):
Alphanumeric (mixed case):
Numbers only:
Base32 (Crockford):
Collision Probability
With default settings (31 characters, length 8):
- Total possibilities: 31^8 = ~852 billion
- At 1 million records: collision probability < 0.0001%
- At 10 million records: collision probability < 0.001%
Increase length for larger datasets:
- Length 6: ~887 million combinations
- Length 8: ~852 billion combinations
- Length 10: ~819 trillion combinations
Testing
Requirements
- PHP 8.2+
- Laravel 11.0+ or 12.0+
License
MIT License - see LICENSE file for details.
Credits
Developed by Aware j.d.o.o.
Support
- Issues: GitHub Issues
- Email: [email protected]