wooserv/php-objectid
Generate and work with MongoDB-style ObjectId values in PHP. Create new ObjectIds, parse and validate existing ones, convert to hex strings, and access parts like timestamp for sorting or debugging. Lightweight utility with no database required.
wooserv/php-objectid package provides a lightweight, MongoDB-compatible ObjectId generator, which is ideal for systems requiring distributed ID generation (e.g., microservices, sharded databases, or applications needing globally unique identifiers without external dependencies like UUIDv7).ObjectId generation is faster than UUID libraries (e.g., ramsey/uuid).ObjectId embeds a timestamp, which may conflict with Laravel’s default created_at field if not handled carefully.ObjectId::generate()).boot() to inject ObjectId into models (e.g., protected $primaryKey = 'object_id';).object_id column as binary(12) or string in PostgreSQL/MySQL).ObjectId comparisons (e.g., $where = ['object_id' => new ObjectId($id)]).ObjectId).ObjectId indexing.where('object_id', '>', new ObjectId()) may need raw queries).binary(12) or char(24) columns.ObjectId generation fails?mongodb/mongodb PHP driver’s built-in ObjectId)?incrementing or uuid IDs without justification.logs or audits) to test generation and database storage.object_id as a secondary key alongside existing id during transition.// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('objectid', function () {
return new \Wooserv\ObjectId\ObjectId();
});
}
// app/Models/ObjectIdModel.php
protected $keyType = 'string';
protected $primaryKey = 'object_id';
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->object_id = app('objectid')->generate();
});
}
object_id binary(12) (PostgreSQL) or char(24) (MySQL) column.ObjectId generation via API (e.g., /generate-id endpoint) for external systems.belongsTo with foreignKey).ObjectId comparisons (e.g., DB::raw('object_id > ?', [$boundId])).withIds() to handle ObjectId.Mockery to stub ObjectId generation in unit tests.mongodb/mongodb) may be needed long-term.object_id column drops).ObjectId format (e.g., 24-byte binary vs. 24-char hex).VARCHAR instead of BINARY).created_at.ObjectId::isValid($id) for validation.ObjectId::getTimestamp($id) to inspect creation time.object_id columns are indexed for performance.How can I help you explore Laravel packages today?