- What does juststeveking/laravel-key-factory actually do?
- It generates unique, customisable keys for Eloquent models instead of relying on auto-incrementing integers. You bind a generator class to a model in a config map (for example Ulid::class) and add the HasKeyFactory trait, and the key is assigned when the model is created. The package is a small utility that sits in the model layer, so it does not change your application's identity strategy beyond the models you opt in.
- How do I install laravel-key-factory in a Laravel application?
- Install it with composer require juststeveking/laravel-key-factory, publish the package config, and map your model class to a generator class in that config. Then add the HasKeyFactory trait to the model and make sure the migration column type and length match the generated key format. Because the last release was November 2022, validate the Composer constraints against your current Laravel and PHP versions in a scratch branch before merging.
- Which Laravel and PHP versions does this package support?
- The package is Laravel and Eloquent only, so it does not apply to other PHP frameworks or ORMs. Its last tagged release was November 2022, which means Composer constraints may be stale against current Laravel and PHP lines. Assume incompatibility until you have proven it with a real composer require in a scratch branch, and pin the version if you adopt it.
- Does Laravel already support this with HasUuids and HasUlids?
- Yes. Laravel has shipped the native HasUuids and HasUlids traits since version 9, so the core functionality of generating model keys is largely redundant with the framework. The incremental value here is per-model generator selection and prefixed, customisable key formats. Benchmark any adoption against the native traits and a roughly one-hundred-line first-party implementation before adding a dependency.
- Can I customise the key prefix, length, and allowed characters?
- Yes. Each generator can be configured with a prefix, a length, and the set of allowed characters, and the config file maps model classes to those generators. That makes it straightforward to produce prefixed public IDs or order-number-style keys per model. Confirm the resulting format matches a schema you already own, because it forces a column-type decision such as ulid, uuid, or a prefixed string column.
- Is this package for primary keys or for API tokens?
- The name and description have been read both ways, and they are different decisions with different risk profiles. It generates Eloquent model keys, which fits identity and public ID use cases, and the blurb mentions API tokens and order numbers as examples. If your real need is customer-facing credentials, use Sanctum or Passport instead, because a key generator is the wrong tool for that job.
- Is laravel-key-factory still maintained?
- Signals are weak: the last release was November 2022, the repository has around six stars, no visible dependents, a single maintainer, and no visible security policy. The code surface is tiny, so the blast radius of a defect is small, but an unmaintained dependency in a correctness-critical path is a real concern. If you adopt it, pin the version and name an owner for the fork in writing.
- What are the collision and concurrency guarantees?
- Key generation is correctness-critical, so you need to know whether uniqueness is enforced by the generator, by a database unique index, or both. Without a unique index you risk duplicate keys under concurrent requests and bulk inserts. Also check column length, index, and collation alignment, and whether the generated format is sortable for keyset pagination or opaque random.
- How should I test that key generation is working correctly?
- Start with a smoke test on a single non-critical model, asserting that Model::create([])->id returns a 26-character ULID rather than an integer. Then add tests for collision handling, concurrent creation, and bulk inserts, plus a migration assertion on column type and length. Also watch for conflicts between the trait and existing model events that could silently alter the model lifecycle.
- What are the alternatives to laravel-key-factory?
- The main alternatives are Laravel's native HasUuids and HasUlids traits, Sanctum or Passport if the requirement is actually credentials, or a small first-party implementation you own. Scope the package narrowly to a small number of models with a specific key requirement rather than treating it as an application-wide identity standard, where the framework default should win. If the format is the only real requirement, roughly one hundred lines of owned code is often the cheaper answer.