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
Laravel Prefixed Ids

Laravel Prefixed Ids Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Friendly prefixed IDs for Laravel models

Frequently asked questions about Laravel Prefixed Ids
How do I install and set up spatie/laravel-prefixed-ids in a Laravel project?
Run `composer require spatie/laravel-prefixed-ids`, then add the `HasPrefixedId` trait to your Eloquent models. No migrations are required if using the default `id` column, but you can publish migrations if you need a dedicated `prefixed_id` column. The package auto-registers with Laravel’s service provider.
Can I use prefixed IDs alongside existing numeric IDs in the same model?
Yes. The package supports dual-mode operation: you can query models by prefixed ID (e.g., `User::findByPrefixedId('user_abc123')`) or fall back to raw IDs. This is useful during migration phases or if some systems still rely on numeric IDs.
What Laravel and PHP versions does this package support?
The package requires PHP 8.0+ and Laravel 9+. It leverages modern PHP features like named arguments and attributes, so ensure your project meets these requirements before installation.
How do I customize the prefix format for my model IDs?
Use the `getPrefixedIdPrefix()` method in your model to define a custom prefix (e.g., `return 'user_'`). You can also configure global defaults in the `config/prefixed-ids.php` file after publishing the config with `php artisan vendor:publish`.
Will prefixed IDs impact query performance in large-scale applications?
Prefixed IDs introduce minimal overhead since they rely on string parsing during lookups. For high-throughput operations, ensure your database indexes the prefixed column (if used) and consider caching prefix-to-model mappings in Redis for frequent queries.
How do I handle existing records when migrating to prefixed IDs?
Run a data migration script to backfill prefixed IDs for existing records. Use the `prefixed_id` column (if added) or generate them on-the-fly via accessors. During transition, maintain dual support for both raw and prefixed IDs in APIs and queries.
Can I use UUIDs or other ID generators instead of auto-incrementing integers?
Yes. The package works with any ID type (UUIDs, strings, etc.) as long as it’s stored in the `id` column. Configure your model’s `getPrefixedId()` method to generate the base ID (e.g., `Str::uuid()`) before prefixing.
How do I resolve conflicts if two models use the same prefix (e.g., 'user_' and 'customer_')?
Avoid overlapping prefixes by using unique, domain-specific prefixes (e.g., `user_` for users, `inv_` for invoices). The package doesn’t enforce uniqueness, so plan prefixes carefully—especially if merging systems later. Use `PrefixedIds::getModelClass()` to dynamically resolve models.
Does this package work with Laravel Scout for search or third-party APIs?
Yes, but ensure your search queries and external APIs account for prefixed IDs. For Scout, update your searchable fields to include prefixed IDs or handle conversions in your search logic. For APIs, expose endpoints that accept both prefixed and raw IDs during migration.
Are there alternatives to spatie/laravel-prefixed-ids for prefixed IDs in Laravel?
For simple use cases, you could manually add accessors/mutators to your models. However, this package offers built-in model resolution, config flexibility, and Eloquent integration out of the box. Other options include custom traits or packages like `laravel-uuid`, but none specialize in prefixed IDs as comprehensively.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport