[英]keeping the order of invoices after deletion in mysql - laravel
Whenever a user is inserting an invoice on our laravel app, it numbers the respective invoice.每当用户在我们的 laravel 应用程序中插入发票时,它都会为相应的发票编号。 so if they enter the first invoice ever, then it will be invoice 1, second invoice will be invoice 2 and so on
因此,如果他们输入第一张发票,那么它将是发票 1,第二张发票将是发票 2,依此类推
The number (1, 2, 3, ..., x) is basically the id (which is the primary key of the invoices table).数字 (1, 2, 3, ..., x) 基本上是 id(它是 invoices 表的主键)。 But now let's say that there are 30 invoices and he wants to delete the 25th one, the next one that will be inserted won't be number 25, it will be 31, because it creates a new record in the table, and it should be 25 because that's the one that's missing
但是现在假设有 30 张发票,他想删除第 25 张,下一个插入的不是 25 号,而是 31 号,因为它在表中创建了一条新记录,应该25岁,因为那是缺少的那个
My question is, how can I automatically set the next number of the invoice to be the first one that's missing?我的问题是,如何自动将发票的下一个编号设置为丢失的第一个? I have a feeling that I should create a new column which indicates the number of the invoice.
我有一种感觉,我应该创建一个新列来指示发票的编号。 This is the code that's creating the invoice:
这是创建发票的代码:
$invoice = new \App\Models\Invoice();
$invoice->provider_id = $request->input('provider-select');
$invoice->number = $request->input('document-number');
$invoice->document_date = $request->input('document-date');
$invoice->due_date = $request->input('due-date');
$invoice->discount_procent = $request->input('discount-procent');
$invoice->discount_value = $request->input('discount-value');
$invoice->total = $request->input('total-value');
$invoice->insertion_date = $request->input('insertion-date');
$invoice->save();
Should I create a new column called numbering
and assign each invoice a number in ascending order, and when one invoices gets delete, to detected the one that's missing and then assign the next invoice with the number and filter them by numerotate
and not by id
?我是否应该创建一个名为
numbering
的新列并按升序为每张发票分配一个编号,当一张发票被删除时,检测到丢失的一张,然后为下一张发票分配该编号并按numerotate
而不是按id
过滤它们?
Yes,rather you should make a new field.是的,您应该创建一个新领域。 You should not touch the primary key handling.
您不应该触摸主键处理。
You can make a new field,let's say sequence
.您可以创建一个新字段,比如说
sequence
。 Before inserting the new data check the missing sequence in invoice
table and assign it to that sequence.在插入新数据之前,检查
invoice
表中缺少的序列并将其分配给该序列。
EDIT - If you want to generate year wise sequence, you need to maintain a year
column and for retrieve records use both sequence
and year
column.编辑- 如果您想生成按年排序,您需要维护一个
year
列,并为检索记录使用sequence
和year
列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.