Comments & Discussion
G
Isn't it better to use the observer when adding an invoice? If the invoice number is NULL then generate a sequential number. In observer you can use the updating method...
DB
public function getFullInvoiceNumberAttribute(){ return 'ABC-' . str_pad($this->id, 5, '0', STR_PAD_LEFT);} public function setInvoiceNumberAttribute(){ $this->attributes['invoice_number'] = App\Models\Invoice::max('invoice_number') + 1;}
in Laravel 11,
App\Models\Invoice::create(['user_id' => 1, 'paid_total' => 200, 'invoice_number' => 0]); = App\Models\Invoice {#6539 user_id: 1, paid_total: 200, invoice_number: 0, updated_at: "2024-07-22 22:11:33", created_at: "2024-07-22 22:11:33", id: 5, } why invoice_number still 0 ? t
M
With Laravel 11, you need to use:
https://laravel.com/docs/11.x/eloquent-mutators#accessors-and-mutators
As the syntax has changed
This is a beautiful explanation. Much appreciated.