PHP code example of linushstge / number-pool

1. Go to this page and download the library: Download linushstge/number-pool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

linushstge / number-pool example snippets

 shell
php artisan make:migration CreateNumberPool
 php


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('number_pool', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique()->index();
            $table->bigInteger('number');
            $table->string('description')->nullable();
            $table->dateTime('created_at');
            $table->dateTime('updated_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('number_pool');
    }
};
 php

$numberPool = new NumberPool([
   'key' => 'invoice.number',
   'number => 999, // latest persisted number
   'description' => 'Pool for generating unique ascending invoice numbers'
]);
$numberPool->save();
 php
public function numberPoolStepSize(): int
{
    // return any positive integer
    return rand(10, 50);
}
 php


class Invoice
{
    // [..]

    protected static function booted()
    {
        static::creating(function ($invoice) {
        
            // your unique incremented number from your number pool is already 
            //available before the transaction has been committed.
            
            $uniqueNumber = $invoice->number;
        });
    }
}