You can use sprintf or vsprintf to format the number.
https://www.php.net/manual/en/function.sprintf.php
// R2022/2023-00001
sprintf('R%s/%s-%05d', 2022, 2023, 1);
Then define a model event to generate the number when an invoice is created.
https://laravel.com/docs/9.x/eloquent#events-using-closures
Example
// migration
public function up()
{
Schema::create('invoices', function (Blueprint $table) {
$table->id();
$table->string('number')->nullable();
$table->date('date');
$table->timestamps();
});
}
// controller
class InvoiceController extends Controller
{
public function store(Request $request)
{
Invoice::create([
'date' => $request->date('date'),
]);
// ...
}
}
// model
class Invoice extends Model
{
protected $casts = [
'date' => 'date',
];
protected static function booted()
{
static::created(function ($invoice) {
$invoice->number = $invoice->generateNumber();
$invoice->save();
});
}
public function generateNumber()
{
return vsprintf('R%s/%s-%05d', [
$this->date->format('Y'),
$this->date->addYear()->format('Y'),
self::whereBetween('date', [
$this->date->startOfYear(),
$this->date->endOfYear()
])->count() + 1,
]);
}
}