我正在使用Laravel Eloquent方法将数据插入表中,我想在用户为admin时增加invoice_no,如何使用laravel Eloquent实现此目的
$user = $request->input('user');
$add_order = new AddOrder;
if($user=="admin")
{
$add_order->invoice_no = 0;
//$add_order->invoice_no = "latest invoice_no in database + 1"
}
$add_order->invoice_prefix = 'NGW';
$add_order->store_name = 'Test Store';
$save_order = $add_order->save();
答案 0 :(得分:0)
您必须执行类似的操作。
$user = $request->input('user');
$add_order = new AddOrder;
$latestAddOrder = $add_order->latest()->first();
if($user=="admin"){
$add_order->invoice_no = $latestAddOrder->invoice_no + 1;
}
$add_order->invoice_prefix = 'NGW';
$add_order->store_name = 'Test Store';
$save_order = $add_order->save();
在这里,我从数据库中检索了最后一行。