A package for generating random BIGINT
IDs as primary keys in Laravel models. These IDs are compatible with MySQL BIGINT
columns and JavaScript's MAX_SAFE_INTEGER
.
Systems using distributed databases like Cloud Spanner or Firestore shouldn't use incremental id's to avoid bottlenecks. UUID can be used as an alternative but long strings can cause performance issues and UX issues. Random BIGINT is a middle ground between incremental id and UUID.
Install the package via Composer:
composer require firevel/model-random-id
Ensure your database table uses a BIGINT
primary key. For example:
$table->bigInteger('id')->unsigned()->primary();
Add the trait to your model:
use \Firevel\ModelRandomId\HasRandomId;
Disable auto-incrementing for the primary key:
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
While random number generation reduces the risk of ID collisions, it is not entirely immune to conflicts. The more IDs you generate, the higher the chance of a collision due to the Birthday Paradox—where random sampling within a finite range (in this case, BIGINT) increases the likelihood of overlap as the number of samples grows. For use cases involving millions of rows or extremely high throughput, consider pre-generating a list of unique IDs and distributing them to avoid runtime conflicts. This approach ensures scalability while preserving randomness.