Php Laravel 5.2中的自动发票编号生成

Php Laravel 5.2中的自动发票编号生成,php,sql,laravel,laravel-5.2,Php,Sql,Laravel,Laravel 5.2,我正在使用laravel 5.2构建一个发票应用程序 我想创建自动递增的发票号。意思是 如果我打开“创建发票”页面,我将自动获得#0001,因此当我保存发票时,在“我的发票编号”字段下,它将是0001,然后当我创建另一张发票时,控制器将检查数据库中的最后一张发票编号,并自动给我#0002 有没有关于如何做到这一点或类似的想法 谢谢按降序从数据库中获取最后一条记录 $invoice = new Invoice(); $lastInvoiceID = $invoice->orderBy('i

我正在使用laravel 5.2构建一个发票应用程序

我想创建自动递增的发票号。意思是

如果我打开“创建发票”页面,我将自动获得#0001,因此当我保存发票时,在“我的发票编号”字段下,它将是0001,然后当我创建另一张发票时,控制器将检查数据库中的最后一张发票编号,并自动给我#0002

有没有关于如何做到这一点或类似的想法


谢谢

按降序从数据库中获取最后一条记录

$invoice = new Invoice();

$lastInvoiceID = $invoice->orderBy('id', DESC)->pluck('id')->first();
$newInvoiceID = $lastInvoiceID + 1;
这将返回最后一个ID。然后向其添加1,并在前端显示它
{{{$newInvoiceID}

可能的问题:如果两个用户同时创建发票怎么办?保存后,如果发票编号是唯一标识符,则编号将不同

一个选项是为每个用户创建发票

$invoice = new Invoice();

$lastInvoiceID = $invoice->where('user_id', '=', Auth::user()->id)->orderBy('invoice_id', DESC)->pluck('invoice_id')->first();
$newInvoiceID = $lastInvoiceID + 1;

按降序从数据库中获取最后一条记录

$invoice = new Invoice();

$lastInvoiceID = $invoice->orderBy('id', DESC)->pluck('id')->first();
$newInvoiceID = $lastInvoiceID + 1;
这将返回最后一个ID。然后向其添加1,并在前端显示它
{{{$newInvoiceID}

可能的问题:如果两个用户同时创建发票怎么办?保存后,如果发票编号是唯一标识符,则编号将不同

一个选项是为每个用户创建发票

$invoice = new Invoice();

$lastInvoiceID = $invoice->where('user_id', '=', Auth::user()->id)->orderBy('invoice_id', DESC)->pluck('invoice_id')->first();
$newInvoiceID = $lastInvoiceID + 1;

自动增加MySQL表中的发票号字段(或仅使用“ID”作为发票号:

CREATE TABLE invoices (
    ID int NOT NULL AUTO_INCREMENT,
    invoice_number NOT NULL AUTO_INCREMENT
)
每次向表中添加发票时,MySQL都会自动为其分配下一个数字。因此,如果表中的最后一张发票是3,则插入的下一张发票将被分配为4


如果您想显示像#0003和#0004这样的数字,只需查询数据库中的发票号码并用PHP格式化该号码。

自动递增MySQL表中的发票号码字段(或者只使用“ID”作为发票号码:

CREATE TABLE invoices (
    ID int NOT NULL AUTO_INCREMENT,
    invoice_number NOT NULL AUTO_INCREMENT
)
每次向表中添加发票时,MySQL都会自动为其分配下一个数字。因此,如果表中的最后一张发票是3,则插入的下一张发票将被分配为4


如果您想显示像#0003和#0004这样的数字,只需查询数据库中的发票号并用PHP格式化即可。

这是我的控制器,如何添加代码?公共函数save#u invoice(Request$Request){$post=$Request->all()//var_dump($post);exit();$data=array('client_id'=>post['client_id'],'invoice_number'=>post[$this->invoice_number()],'date_created'=>post['date_created'],'due_date'=>post['due_date'],'status'=>post['status','payment_method'=>post['payment_method'],'currency'=>post['currency'];$j=DB::table('invoice_headers')->insertGetId($data);if($j>0){for($i=0;$i$post['item'][$i],'description'=>$post['description'][$i],'quantity'=>$post['quantity'][$i],'price'=>$post[$price'[$i],“项目折扣”=>$post[$i],“总计”=>$post['total'][$i],);DB::table('invoice_Bodys')->insert($datadetail);}}}$datafinal=array('invoice_id'=>$j,'subtotal'=>$post['subtotal'],'折扣'=>$post['折扣'],'total'=>$post['grand-total'],'Payment'=>,'balance'=>$post['balance']);DB::table('invoice_footers')->insert($datafinal);return redirect()->back();}在返回创建发票视图的函数所在的位置,将我的代码放在那里并进行相应修改。然后,确保返回发票编号以及所需的任何其他数据。这是我的控制器,如何添加代码?公共函数save_invoice(Request$Request){$post=$Request->all();//var_dump($post);exit();$data=array('client_id'=>$post['client_id'],'invoice_number'=>$post[$this->invoice_number()],'date_created'=>$post['date_created'],'due_date'=>$post['due_date'],'status'=>$post['status'],'payment(付款方法'=>$post['payment(付款方法'],'currency'=>$post['currency']);$j=DB::table('invoice_headers')->insertGetId($data);if($j>0){for($i=0;$i$j,'item'=>$post['item'][$i],'description'=>$post['description']i],'quantity'=>$post['quantity'['quantity']i],'price'=>$post['price'][$i],'item_折扣'=>post['item_折扣'][$i],'total'=>post['total'][$i],);DB::table('invoice_body')->insert($datadetail);}}}$datafinal=array('invoice_id'=>j美元,'subtotal'=>post['subtotal'],'discount'>$post['discount'],'total'=>post['discount','discount''>$post['discount'],'total'''=>post['grand'.'total''','balance'=>$post['balance']);DB::table('invoice_footers')->insert($datafinal);return redirect()->back();}在返回创建发票视图的函数所在的位置,将我的代码放在那里并进行相应的修改。然后,确保返回发票编号以及所需的任何其他数据。