- How do I use this package in Laravel to replace auto-increment IDs?
- You’ll need to modify your Eloquent models to use `object_id` as the primary key by setting `protected $primaryKey = 'object_id';` and casting it to `object` type. Store the column as `binary(12)` in MySQL or `char(24)` in PostgreSQL. Note that this isn’t a drop-in replacement—you’ll also need to handle queries with raw SQL for comparisons.
- Does this package work with Laravel’s query builder for WHERE clauses?
- No, Laravel’s query builder doesn’t natively support ObjectId comparisons. You’ll need to use raw SQL (e.g., `DB::select('SELECT * FROM table WHERE object_id = ?', [$id->toHex()])`) or cast the ObjectId to a hex string for string-based queries. This is a common limitation when mixing MongoDB-style IDs with SQL databases.
- Can I use this for time-sorted queries like MongoDB’s ObjectId?
- Yes, ObjectId embeds a timestamp, so you can sort by ID in ascending order for approximate creation-time ordering. However, this conflicts with Laravel’s `created_at` field if both exist. Use either the ObjectId’s timestamp (via `ObjectId::getTimestamp()`) or disable automatic timestamp generation in your models to avoid redundancy.
- What Laravel versions does this package support?
- The package itself is framework-agnostic but requires PHP 8.0+. It’s compatible with Laravel 8+ since it doesn’t rely on Laravel-specific features. Test thoroughly in your Laravel version, as some edge cases (like model events) may need manual adjustments. No official Laravel-specific documentation is provided, so assume general PHP compatibility.
- How do I generate ObjectIds in bulk for seeding or testing?
- Use a loop with `ObjectId::generate()` or leverage batch generation if the package supports it (check the latest docs). For testing, pre-generate IDs and store them in a fixture file. Avoid generating IDs dynamically in tests unless you need uniqueness—consider using a mock or factory to simulate IDs instead.
- Is this package secure for production use? Are there risks with time-based IDs?
- The package is secure for basic use, but time-based IDs expose approximate creation times, which could be a risk in high-security applications. If needed, mask the timestamp or use a hybrid approach (e.g., UUID prefix + ObjectId). Always validate IDs with `ObjectId::isValid()` before use to prevent injection or malformed data.
- How do I migrate an existing Laravel app to use ObjectId?
- Start with a dual-write phase: add an `object_id` column to your tables and populate it during transitions. Use model observers or database triggers to generate ObjectIds alongside existing IDs. For critical systems, test thoroughly with a subset of data before full migration. Consider a phased rollout (e.g., non-critical tables first).
- Are there alternatives to this package for Laravel?
- Yes. For MongoDB interoperability, use the official `mongodb/mongodb` PHP driver, which includes built-in ObjectId support. For time-sortable UUIDs, consider `ramsey/uuid` with UUIDv7. If you need URL-safe IDs, `ramsey/uuid` or `ulid/ulid` are better choices. This package is lightweight but lacks active maintenance compared to those alternatives.
- How do I store ObjectId in PostgreSQL or MySQL?
- In MySQL, use a `binary(12)` column type to store the raw ObjectId bytes. In PostgreSQL, use `char(24)` for hex strings or `bytea` for binary data. Avoid storing as text unless you convert to hex first. Indexing works best with binary storage—ensure your queries use raw comparisons (e.g., `WHERE object_id = UNHEX('...')` in MySQL).
- Can I use this package with Laravel Scout for full-text search?
- No, this package doesn’t include Laravel Scout adapters. Scout expects string-based IDs for indexing, so you’d need to convert ObjectIds to hex strings manually (e.g., `$model->object_id->toHex()`). For advanced use, consider extending Scout’s engine or using a custom search solution that handles binary data.