spatie/laravel-prefixed-ids
Generate friendly Stripe-like prefixed IDs for Laravel Eloquent models (e.g., user_xxx). Add a trait to models, create and store prefixed IDs, and resolve models from a prefixed ID via findByPrefixedId or automatic model detection.
user_, order_, token_).prefixed_id column).id columns remain untouched; prefixed IDs are optional per model.findByPrefixedId) and fallback to raw IDs (configurable).user_, inv_) and ID generation (e.g., UUID, incrementing) are configurable.user_ and customer_ if merged later).user_) or dynamic (e.g., tenant-aware like tenant123_user_)?1, abc123) coexist with new prefixed IDs during transition?GET /users/1 and GET /users/user_abc123)?id column).tests/ directory.User, Order).prefixed_id column (via migration) and backfill data (batch job).findByPrefixedId + fallback to find)./users/user_abc123).user-abc123 instead of 123).IdGenerator interface.SoftDeletes trait.dispatch(new ProcessOrder(user_abc123))).| Phase | Task | Dependencies |
|---|---|---|
| Prep | Audit models, validate dependencies, design prefixes. | Business stakeholder alignment. |
| Setup | Install package, publish config, configure prefixes. | Laravel project ready. |
| Pilot | Implement on 1–2 models, test findByPrefixedId. |
Basic CI/CD pipeline. |
| API Integration | Update endpoints to handle prefixed IDs. | Pilot model validation. |
| Frontend Sync | Replace raw IDs in UI/URLs. | API changes. |
| Data Migration | Backfill prefixed_id for existing records (if needed). |
Pilot success. |
| Monitoring | Add alerts for ID generation failures. | Observability tools in place. |
| Deprecation | Phase out raw ID support in APIs. | Full adoption confirmed. |
config/prefixed-ids.php).prefixed_id column), but migrations should be idempotent.NoPrefixedModelFound exceptions for user-facing messages.user_ vs. USER_).id_123).prefixed_id is indexed if used for queries.| Risk | Mitigation Strategy |
|---|---|
| ID Collisions | Use UUIDs or incrementing IDs with prefixes (e.g., user_1, user_2). |
| Migration Data Loss | Back up databases before backfilling prefixed_id. |
| API Breaking Changes | Deprecate old endpoints with versioning (e.g., /v1/users/1, /v2/users/user_1). |
How can I help you explore Laravel packages today?