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

Eloquent Keygen Laravel Package

jetcod/eloquent-keygen

View on GitHub
Deep Wiki
Context7

Eloquent KeyGen

Code coverage

This is a Laravel package providing seamless integration with Snowflake ID generation for Eloquent models. Simplify primary key management with automatic generation using a highly distributed unique identifier system. Enhance your application's scalability and efficiency with this easy-to-use package for Laravel Eloquent models.

Installation

To install jetcod/deloquent-keygen, you can use Composer, the dependency manager for PHP. Run the following command in your terminal:

composer require jetcod/eloquent-keygen

Then you need to publish the config file:

php artisan vendor:publish --provider="eloquent-key-generator-config"

How to use?

To use the package, follow these steps:

  • Extend Your Model:

Instead of extending Illuminate\Database\Eloquent\Model, extend Jetcod\Eloquent\Model in your Eloquent model. This will enable automatic ID generation using the Snowflake algorithm.

<?php

namespace App\Models;

use Jetcod\Eloquent\Model as EloquentModel;

class YourModel extends EloquentModel
{
    // Your model code here
}
  • Using Trait:

If you don't want to extend the Eloquent model, or you want to apply the Snowflake ID generation to one model, you can use the Snowflake trait in your model class.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Jetcod\Eloquent\Traits\Snowflake;

class YourModel extends EloquentModel
{
    use Snowflake;
}
  • Disable Snowflake Generation for a Model:

If you want to disable the Snowflake ID generation for a particular model, you can add a snowflake method to that model returning false.

<?php

namespace App\Models;

use Jetcod\Eloquent\Model as EloquentModel;

class AnotherModel extends EloquentModel
{
    protected function snowflake(): bool
    {
        return false;
    }

    // Your model code here
}

The snowflake method returns a boolean value indicating whether the Snowflake ID generation should be enabled or disabled for that model.

  • Configuration for distributed system:

The package uses the following configuration to generate the distributed unique identifier:

<?php

use Godruoyi\Snowflake\LaravelSequenceResolver;

return [
    'attributes' => [
        /*
        |------------------------------------------------------------------
        | This represents the datacenter id used to generate snowflake ids.
        |------------------------------------------------------------------
        */

        'datacenter' => env('SNOWFLAKE_DATACENTER_ID', 1),

        /*
        |--------------------------------------------------------------
        | This represents the worker id used to generate snowflake ids.
        |--------------------------------------------------------------
        */

        'worker' => env('SNOWFLAKE_WORKER_ID', 1),

        /*
        |--------------------------------------------------------------------------------------------------------
        | This represents the sequence resolver class. It is to ensure that sequence-number generated in the same
        | millisecond of the same node is unique.
        |
        | Avaialable resolvers:
        |   1. Godruoyi\Snowflake\RandomSequenceResolver
        |   2. Godruoyi\Snowflake\FileLockResolver
        |   3. Godruoyi\Snowflake\LaravelSequenceResolver
        |--------------------------------------------------------------------------------------------------------
        */

        'sequence_resolver' => env('SNOWFLAKE_SEQUENCE_RESOLVER', LaravelSequenceResolver::class),

        'file_lock_directory' => env('SNOWFLAKE_File_LOCK_DIRECTORY', null),  // Default is null, means use <app_path>/storage/snowflake directory.
    ],
];

Support the Project

If Eloquent KeyGen saves you time or is useful in your work, you can support its continued development through Buy Me a Coffee. Support helps with ongoing maintenance, bug fixes, documentation, compatibility updates, and future improvements. Contributions are appreciated but entirely optional, and the project remains available to everyone.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata