Search code examples
laravellaravel-5laravel-5.3

What is the best way to generate unique number in Laravel?


What is the best way to What is the best way to generate unique number in Laravel? Is there standard function from box to generate unique number in Laravel?


Solution

  • It's a little unclear what you're trying to accomplish with this, but here's a stab at it.

    Create a Number model with the following schema:

    Schema::create('numbers', function (Blueprint $table) {
        $table->increments('id');
    });
    

    Leverage its auto-increment ID as your unique number. These will be sequential, not random numbers.

    $number = App\Number::create();
    $number->id; // a guaranteed unique number