Giter Site home page Giter Site logo

laravel-nanoid's Introduction

Laravel Nanoid

Introduction

A simple drop-in solution for providing nanoid support for the IDs of your Eloquent models. (Stripe-like IDs)

Installing

composer require malico/laravel-nanoid

Usage

Use nanoid within your model, use the trait HasNanoids within your model like.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
+ use Malico\LaravelNanoid\HasNanoids;

class Book extends Model{
    use HasFactory;
+   use HasNanoids;
}

Your migration file should like.

// migration file
public function up()
{
    Schema::create('test_models', function (Blueprint $table) {
-       $table->id();
+       $table->string('id')->primary();
        //
        $table->timestamps();
    });
}

To create a new migration, use the artisan command make:nanoid-migration. All arguments work the same as the make:migration command.

Options

  1. Prefix: To Specify a Prefix for the IDs, you can specify a prefix by add `nanoPrefix' property to your model class.
  2. Same applies for the length of the ID.
<?php

    /** @var array|int */
    protected $nanoidLength = 10;
    // id will be of length 10
    // specifying to array. e.g [10, 20] will generate id of length 10 to 20
    // or
    public function nanoidLength(): array|int
    {
        // [10,20]
        return 10;
    }

    /** @var string */
    protected $nanoidPrefix = 'pl_'; // id will look: pl_2k1MzOO2shfwow ...
    // or
    public function nanoidPrefix(): string
    {
        return 'pay_'; // pay_2MII83829sl2d
    }

    /** @var string */
    protected nanoidAlphabet = 'ABC';
    // or
    public function nanoidAlphabet(): stirng {
        return 'ABC'; // pay_ACBACB
    }

Check the upgrade guide if you're upgrading from 0.x

Author

Ndifon Desmond Yong

laravel-nanoid's People

Contributors

yondifon avatar pietrantonio91 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.