Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bits Laravel Package

glhd/bits

Generate unique 64‑bit IDs for distributed systems in PHP. Create Twitter Snowflake, Sonyflake, or custom bit-sequence identifiers with configurable worker/datacenter IDs and epoch for compact, time-ordered, collision-resistant IDs across multiple servers.

View on GitHub
Deep Wiki
Context7

Bits is a PHP library for generating unique 64-bit IDs in distributed systems using configurable bit sequences. Create Twitter Snowflake, Sonyflake, or custom identifier layouts while keeping IDs compact, sortable, and safe across multiple workers and datacenters.

Configure BITS_WORKER_ID, BITS_DATACENTER_ID, and a custom BITS_EPOCH to avoid collisions and control the usable time range of your IDs.

  • Generate Snowflake/Sonyflake-style IDs (or define your own layout)
  • Compact, time-ordered 64-bit identifiers
  • Supports multi-worker, multi-datacenter deployments
  • Configurable epoch to tune lifespan and ordering
  • Simple Composer install: composer require glhd/bits
Frequently asked questions about Bits
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport