PHP code example of welman91 / filament-record-number-generator

1. Go to this page and download the library: Download welman91/filament-record-number-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/ */

    

welman91 / filament-record-number-generator example snippets


use Welman91\FilamentRecordNumberGenerator\FilamentRecordNumberGeneratorPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugin(
            FilamentRecordNumberGeneratorPlugin::make()
                ->navigationGroup('Settings') // optional, defaults to "Settings"
        );
}

use Welman91\FilamentRecordNumberGenerator\Concerns\HasNumbering;

class Invoice extends Model
{
    use HasNumbering;

    // Optional: explicitly declare which attributes to auto-number.
    // If omitted, the trait auto-detects from the numbering_sequences table.
    protected array $numberingFields = ['invoice_number'];
}

use Welman91\FilamentRecordNumberGenerator\Models\NumberingSequence;

NumberingSequence::create([
    'company_id'              => 1,           // tenant scope (null for global)
    'name'                    => 'Invoice Number',
    'model_type'              => \App\Models\Invoice::class,
    'attribute'               => 'invoice_number',
    'pattern'                 => 'INV-{year}-{sequence:4}',
    'reset_frequency'         => 'yearly',    // never, yearly, monthly, daily
    'fiscal_year_start_month' => 1,           // 1 = January
    'is_gap_free'             => true,
    'is_active'               => true,
    'initial_value'           => 1,
]);

NumberingSequence::create([
    // ...
    'pattern'       => '{branch}-{year}-{sequence:4}',
    'custom_tokens' => [
        'branch' => 'attribute:branch.code', // {branch} resolves to $model->branch->code
    ],
]);

use Welman91\FilamentRecordNumberGenerator\Contracts\TokenResolver;

class DepartmentCodeResolver implements TokenResolver
{
    public function resolve(string $token, ?string $argument, array $context): string
    {
        return $context['model']->department->short_code ?? 'GEN';
    }

    public function supports(string $token): bool
    {
        return $token === 'dept';
    }
}

'custom_resolvers' => [
    \App\NumberingResolvers\DepartmentCodeResolver::class,
],

Invoice::create([
    'invoice_number' => 'MANUAL-001', // trait won't overwrite this
    // ...
]);

use Welman91\FilamentRecordNumberGenerator\Services\NumberingEngine;

$engine = app(NumberingEngine::class);
$number = $engine->generate($invoice);

$engine = app(NumberingEngine::class);
$preview = $engine->preview($invoice);
// e.g. "INV-2026-0043"

$invoice->generateNumber('invoice_number');
$invoice->previewNextNumber('invoice_number');

use Welman91\FilamentRecordNumberGenerator\Events\NumberGenerated;

Event::listen(NumberGenerated::class, function (NumberGenerated $event) {
    // $event->model
    // $event->attribute
    // $event->generatedNumber
    // $event->sequence
});

FilamentRecordNumberGeneratorPlugin::make()
    ->sequenceResource(true)
    ->navigationGroup('Administration')
bash
php artisan vendor:publish --tag=filament-record-number-generator-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=filament-record-number-generator-config
bash
php artisan vendor:publish --tag=filament-record-number-generator-translations
bash
php artisan test --filter=NumberingEngine
php artisan test --filter=PatternParser