- How do I install and configure **glhd/bits** for Laravel?
- Run `composer require glhd/bits` to install. Configure `BITS_WORKER_ID`, `BITS_DATACENTER_ID`, and `BITS_EPOCH` in your `.env` file. Each worker/datacenter must have unique IDs (0-31) to avoid collisions. The epoch defaults to 2023-01-01 but can be adjusted for your needs.
- Can I use **glhd/bits** with Laravel Eloquent models?
- Yes. Add the `HasSnowflakes` trait to your model or use `$casts = ['id' => Snowflake::class]` in your model. The package automatically handles casting between 64-bit IDs and strings, ensuring compatibility with Laravel’s query builder and JSON responses.
- What Laravel versions does **glhd/bits** support?
- The package is tested up to Laravel 13 and is backward-compatible with Laravel 11+. It requires PHP 8.1+ for named arguments and attributes. No database schema changes are needed, making it database-agnostic (MySQL, PostgreSQL, SQLite).
- How do I generate Snowflake/Sonyflake-style IDs in Laravel?
- Use the `snowflake_id()` helper function in your code, or leverage the `HasSnowflakes` trait for Eloquent models. For Sonyflake-style IDs, configure the bitmask in your `Bits` service provider to allocate more bits for sequence resolution instead of timestamps.
- Will **glhd/bits** work in a multi-server or Kubernetes environment?
- Yes, the package supports multi-worker and multi-datacenter deployments. Assign unique `BITS_WORKER_ID` and `BITS_DATACENTER_ID` values per server or pod. For dynamic environments like Kubernetes, consider using environment variables or config files to manage these IDs.
- How do I handle time-based queries with Snowflake IDs?
- Snowflake IDs embed timestamps, so you can query them directly using bitwise operations. For example, use `firstForTimestamp()` to convert a Carbon timestamp into a Snowflake ID for range queries like `WHERE id > firstForTimestamp(now()->subDay())`. This enables efficient time-based filtering.
- What if I need more than 1,024 workers (5-bit datacenter + 5-bit worker)?
- The default configuration limits you to 1,024 workers (32 datacenters × 32 workers). If you exceed this, customize the bitmask in your `Bits` configuration to allocate more bits for worker/datacenter IDs, reducing bits for timestamps or sequences.
- How do I test Snowflake ID generation in Laravel?
- Use `Bits::setTestNow()` to freeze time during tests, ensuring deterministic ID generation. This mimics Carbon’s `setTestNow()` but is specific to Snowflake IDs. Example: `Bits::setTestNow(now()->subDays(1));` to simulate past timestamps.
- Can I use **glhd/bits** with Livewire for client-side ID generation?
- Yes. Register the `SnowflakeSynth` in your `AppServiceProvider` to enable client-side ID generation. This allows Livewire components to generate Snowflake IDs without server round-trips, though IDs should still be cast to strings for JavaScript compatibility.
- What are the alternatives to **glhd/bits** for Laravel, and why choose this one?
- Alternatives include `ramsey/uuid` (UUIDs) or `ulid/ulid` (ULIDs). **glhd/bits** stands out for its compact 64-bit IDs, time-ordered sorting, and native Laravel integration (Eloquent, Livewire, APIs). Unlike UUIDs, Snowflakes are sortable and embed timestamps, reducing storage costs and enabling efficient time-based queries.