XamarinDevil
XamarinDevil

Reputation: 891

Increasing ID in controller to database

This how my id looks like 1-66-0001 on the first day of each day. Now, i want to know how i could increase the last 4 digits by 1. The next id should be 1-66-0002, 1-66-0003 and display the just the last 4 digits in my html table (although the whole id ``1-66-0001` goes into the database) but with my code, i am not getting that.

Controller

if (!empty($invoice) && $invoice->created_at->toDateString() == $today->toDateString())
{
    $today_invoice = $invoice + 1;
} else {
    $today_invoice = $id . '-' . $date . '-0001';
}

Upvotes: 0

Views: 63

Answers (1)

Giulio Bambini
Giulio Bambini

Reputation: 4755

I think it is better to use two "ID" fields: real id and id for database because I don't think it's good database design to use id as primary key with such length and format. So: Id (PK): int(11) RealId (non-clustered index): varchar (10)

As it's string, using string formatting functions you can measure last id number and input it into database.

Also, if "0001" from real id and "1" from primary key are equal and will always be equal, i mean there is only one record with 0001, even without using string formatting functions, you can take primary key value and use it in your realId

if it's 20 -> you will know that last 4 digits should be 0020 and so on.

Upvotes: 3

Related Questions