Laravel: Auto-Increment Numbers like ABC001, ABC002...

preview_player
Показать описание
Pretty typical example for invoice numbers - we need to auto-increment the numbers depending on the prefix of series. In this video, I will show you my approach to it.

Рекомендации по теме
Комментарии
Автор

Using max + 1 is dangerous: if you delete the latest entry and after that create a new entry, it will get the same id as the entry before. Better approach: use the ids only internal and not visible to the frontend. Instead show UUIDs on the frontend. UUIDs are also safer because you cannot guess the ids of an item. But use the integer ids internal because joining between integers is way faster than between strings (that's what UUIDs are in MySQL for example).

zkabadu
Автор

Exactly what i was looking for. The way you adding the number using Creating event on the model and they way you are finding the next incremental number with max. (thumbsup)

rahulk
Автор

Good video! A third and intermediary option would be to have a mutator getFullNumber that executes that logic without requiring the view to do it or a field in the database. This way you can call the full_number as an attribute without the hassle of storing duplicated data.

gpweins
Автор

I needed to implement this in one of my current prokects. Thanks

nabeelyousafpasha
Автор

Hi, nice quick and concise.

Only downside i can see to creating them on the fly and not storing ffull invoice number, is that if you update your prefix, customers previous invoices would change. Which for accounting is not ideal.

jamesmcconnon
Автор

Great video thank you! However, at 1:25, I would recommend to put the logic in a transaction to avoid raise conditions.

IWasHereFirst
Автор

thanks for the video!

you will get a a PDO error in case of creating few invoices at the same time (if you have unique key for number+series_id) or you will get few invoices with the same full number. Reason a DB query to fetch latest invoice number with current series_id takes some time and if you have few requests to create an invoice at the same time, most likely this DB query will return the same result. For big DB tables with few indices it's a pretty common problem for approaches like this.

In order to avoid this problem, you can use locks or set full_number in a queue

alieslapatsin
Автор

Your videos are great and helping a lot. Thank you so much. One request, kindly make a video on Complex relations in laravel Eloquent

siyabdev
Автор

such a nice person you are, Your tips very helpful. After watched this video I implement it in my project. I am very happy after seeing the results.

nadeemahmed
Автор

An important note to mention is this context is that, you need to keep tracking of the serial number of invoices, so the delete invoices could be noted, lets say that you will use soft delete option in laravel, then you has to add withTrashed() scop to Invoice::where('series_id', +1
so you can keep the serial correctly and there are no serial overlap could happened in this case.

stormcorexz
Автор

Thank you very much! I learn a lot through your videos

loican
Автор

you can use int(5) unsigned zerofill in db. You insert value like 1, in db it was 0001

rubl
Автор

Incredible video, please how could it be done so that it returns to number 1 at the beginning of the year or at the beginning of the month ?

urufu
Автор

I love your tutorials sir. I am a beginner learning laravel, I would like to request if you have a tutorial making a full working application step by step. It will help me a lot in advancing my knowledge. Thank you.

fredmhiche
Автор

Thank you very much for your video tutorials.

mgibim
Автор

Your videos are always amazing. Thanks

nipuntharuksha
Автор



example: Put this in the model you want to change the id from


public function getIdAttribute($value)
{
return str_pad($value, 5, '0', STR_PAD_LEFT);
}

drcodr
Автор

my creating event is never fired
static::creating(function ($model) {
error_log('creating');

$model->nice_number = str_pad($model->unit_number, 4, 0, STR_PAD_LEFT);
});

Any ideas?

FvsJ
Автор

Thanks for these man! Awesome tutorials.

DMANCHILD
Автор

Trying to get property 'prefix' of non-object: $model->serie->prefix; how to solve it?

omarml