Download the PHP package calebdw/laraflake without Composer

On this page you can find all versions of the php package calebdw/laraflake. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laraflake

Generate X/Twitter Snowflake identifiers in Laravel.

Test Results Code Coverage License Packagist Version Total Downloads

What are Snowflakes?

Snowflakes are a form of unique identifier devised by X/Twitter and are used by many companies, including Instagram and Discord, to generate unique IDs for their entities.

Some of the benefits of using Snowflakes (over alternatives such as UUID/ULID) include:

Installation

First pull in the package using Composer:

And then publish the package's configuration file:

Configuration

Snowflake Type

The Snowflake type determines the class used to generate Snowflakes.

The default Snowflake type is Godruoyi\Snowflake\Snowflake which uses 41 bits for the epoch, 5 bits for the data center ID, 5 bits for the worker ID, and 12 bits for the sequence. This allows for up to 1024 workers and 4096 unique IDs per worker per millisecond.

You can change the Snowflake type to Godruoyi\Snowflake\Sonyflake which uses 39 bits for the epoch, 16 bits for the machine ID, and 8 bits for the sequence. This allows for up to 65535 machines and 256 unique IDs per worker per 10 milliseconds.

Epoch

The timestamp encoded in the Snowflake is the difference between the time of creation and a given starting epoch/timestamp. Snowflakes use 41 bits and can generate IDs for up to 69 years past the given epoch. Sonyflakes use 39 bits and can generate IDs for up to 174 years past the given epoch.

In most cases you should set this value to the current date using a format of YYYY-MM-DD.

Note: Future dates will throw an error and you should avoid using a date far in the past (such as the Unix epoch 1970-01-01) as that may reduce the number of years for which you can generate timestamps.

Data Center & Worker IDs

Note: This is only used for the Snowflake type.

You can set the data center and worker IDs that the application should use when generating Snowflakes. These are used to ensure that each worker generates unique Snowflakes and can range from 0 to 31 (up to 1024 unique workers).

Machine ID

Note: This is only used for the Sonyflake type.

You can set the machine ID that the application should use when generating Sonyflakes. This is used to ensure that each machine generates unique Sonyflakes and can range from 0 to 65535.

Usage

WARNING: Do not create new instances of the Snowflake generator (as this could cause collisions), always use the Snowflake singleton from the container.

You can generate a Snowflake by resolving the singleton from the container and calling its id method:

This package also provides a snowflake helper function, a Snowflake facade, and a Str::snowflakeId macro for convenience:

Eloquent Integration

Migrations

This package provides a set of migration macros to make it easier to work with Snowflakes in your database schema.

Here's an example:

Models

Next, add the package's HasSnowflakes trait to your Eloquent models:

The trait provides several features for the model's Snowflake columns:

By default, the trait assumes that the model's primary key is a Snowflake. If you have other unique columns that should be treated as Snowflakes, you can override the uniqueIds method to specify them:

If necessary, you can explicitly cast the model's Snowflake columns using the AsSnowflake cast:

Validation

If you need to validate Snowflakes in your application, you can use the Snowflake rule or the Rule::snowflake macro provided by this package:

You can also just use the Str::isSnowflake macro to check if a value is a valid Snowflake:

Sequence Resolver

The sequence resolver is responsible for generating the sequence component of the Snowflake to ensure that numbers generated on the same machine within the same millisecond are unique.

By default, if the application has a cache, then it uses the LaravelSequenceResolver which uses the Laravel cache to store the last sequence number. If the application does not have a cache, then it uses the RandomSequenceResolver which has no dependencies but is not concurrency-safe.

You can override the sequence resolver by binding your own implementation in a service provider:

Please see godruoyi/php-snowflake for more information on the available sequence resolvers and their dependencies.

Contributing

Thank you for considering contributing! You can read the contribution guide here.

License

Laraflake is open-sourced software licensed under the MIT license.

Acknowledgments

Derived from caneara/snowflake which is no longer maintained. The actual Snowflake generation is handled by the excellent godruoyi/php-snowflake library.

Alternatives


All versions of laraflake with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
godruoyi/php-snowflake Version ^3.0
illuminate/contracts Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
illuminate/validation Version ^10.0|^11.0
phpstan/phpstan Version ^1.11
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package calebdw/laraflake contains the following files

Loading the files please wait ....