PHP code example of alimarchal / id-generator

1. Go to this page and download the library: Download alimarchal/id-generator 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/ */

    

alimarchal / id-generator example snippets


// Generate professional IDs instantly
$invoiceId = generateUniqueId('invoice', 'invoices', 'invoice_no');
// Output: INV-20250814-0001

$complaintId = generateUniqueId('complaint', 'complaints', 'complaint_no');
// Output: CMP-20250814-0001

use Illuminate\Support\Facades\DB;

DB::table('id_prefixes')->insert([
    ['name' => 'order', 'prefix' => 'ORD'],
    ['name' => 'receipt', 'prefix' => 'RCP'],
    ['name' => 'estimate', 'prefix' => 'EST'],
]);

// Generate invoice number using 'invoice' type from database
$invoiceId = generateUniqueId('invoice', 'invoices', 'invoice_no');
// Output: INV-20250814-0001

// Generate complaint number
$complaintId = generateUniqueId('complaint', 'complaints', 'complaint_no');
// Output: CMP-20250814-0001

// Generate quotation number
$quotationId = generateUniqueId('quotation', 'quotations', 'quotation_no');
// Output: QTN-20250814-0001

// Generate ID with custom prefix (no database lookup)
$orderId = generateUniqueIdWithPrefix('ORD', 'orders', 'order_no');
// Output: ORD-20250814-0001

$customId = generateUniqueIdWithPrefix('CUST', 'customers', 'customer_id');
// Output: CUST-20250814-0001



namespace App\Http\Controllers;

use Alimarchal\IdGenerator\IdGenerator;
use App\Models\Invoice;
use Illuminate\Http\Request;

class InvoiceController extends Controller
{
    public function store(Request $request, IdGenerator $idGenerator)
    {
        // Method 1: Using database prefix
        $invoiceNumber = $idGenerator->generate('invoice', 'invoices', 'invoice_no');
        
        // Method 2: Using direct prefix
        $orderNumber = $idGenerator->generateWithPrefix('ORD', 'orders', 'order_no');

        $invoice = Invoice::create([
            'invoice_no' => $invoiceNumber,
            'customer_name' => $request->customer_name,
            'amount' => $request->amount,
        ]);

        return response()->json(['invoice' => $invoice], 201);
    }
}

class OrderController extends Controller
{
    public function createOrder(Request $request, IdGenerator $idGenerator)
    {
        $orderNumber = $idGenerator->generate('order', 'orders', 'order_no');
        // Output: ORD-20250814-0001
        
        $invoiceNumber = $idGenerator->generate('invoice', 'invoices', 'invoice_no');
        // Output: INV-20250814-0001
        
        $trackingNumber = $idGenerator->generateWithPrefix('TRK', 'shipments', 'tracking_no');
        // Output: TRK-20250814-0001
        
        // Create order with all numbers...
    }
}

class TicketController extends Controller
{
    public function createTicket(Request $request)
    {
        $ticketNumber = generateUniqueId('ticket', 'tickets', 'ticket_no');
        // Output: TKT-20250814-0001
        
        $ticket = Ticket::create([
            'ticket_no' => $ticketNumber,
            'title' => $request->title,
            'priority' => $request->priority,
        ]);
        
        return response()->json(['ticket' => $ticket], 201);
    }
}



namespace App\Http\Controllers;

use App\Models\Invoice;
use Illuminate\Http\Request;

class InvoiceController extends Controller
{
    public function store(Request $request)
    {
        // Validate request...
        $request->validate([
            'customer_name' => 'umber,
            'customer_name' => $request->customer_name,
            'amount' => $request->amount,
            'created_at' => now(),
        ]);

        return response()->json([
            'message' => 'Invoice created successfully!',
            'invoice' => $invoice
        ], 201);
    }
}

// This package handles this scenario automatically:
// User A and User B create invoices at the EXACT same millisecond

// Without protection:
// User A gets: INV-20250814-0001
// User B gets: INV-20250814-0001 ❌ (DUPLICATE!)

// With this package:
// User A gets: INV-20250814-0001 ✅
// User B gets: INV-20250814-0002 ✅ (UNIQUE!)

// Different prefixes for different environments
// In your AppServiceProvider boot method:

if (app()->environment('production')) {
    DB::table('id_prefixes')->updateOrInsert(
        ['name' => 'invoice'],
        ['prefix' => 'INV']
    );
} else {
    DB::table('id_prefixes')->updateOrInsert(
        ['name' => 'invoice'],
        ['prefix' => 'TEST-INV']
    );
}

// Add new prefix types dynamically
DB::table('id_prefixes')->insert([
    'name' => 'purchase-order',
    'prefix' => 'PO',
    'created_at' => now(),
    'updated_at' => now()
]);

// Use the new prefix
$poNumber = generateUniqueId('purchase-order', 'purchase_orders', 'po_number');
// Output: PO-20250814-0001

// The package oiceId = generateUniqueId('invoice', 'invoices', 'invoice_no');
} catch (\Exception $e) {
    // If normal generation fails, package provides fallback
    // Fallback format: INVOICE-1692012345-1234 (timestamp-based)
    Log::error("ID generation failed: " . $e->getMessage());
    // You still get a unique ID, just less pretty
}

// Solution: Add the prefix to your database
DB::table('id_prefixes')->insert([
    'name' => 'your-type',
    'prefix' => 'YOUR-PREFIX'
]);

// Solution: Ensure your table has the target column
Schema::table('your_table', function (Blueprint $table) {
    $table->string('your_id_column')->unique();
});
bash
php artisan vendor:publish --tag=id-generator-migrations
php artisan migrate