Laravel Elequent在保存前获取ID()

时间:2018-02-23 08:00:11

标签: php laravel laravel-5

我需要获取ID才能在实际创建之前创建,我已经尝试获取Last ID,但是进入了pickle,因为如果删除了所有项目id DB,没有"最后" ID。所以现在我通过在同一Store()函数中创建和立即更新来解决它。不确定这是否会占用大量资源(可能不是),但仅仅是出于好奇和性能的提示,有关如何做得更好的提示?

以下是代码:

      $var = $request['purchase_date'];
      $date = implode("-", array_reverse(explode("/", $var)));
      $year = date('y'); 

      $Purchase = new Purchase;
      $Purchase->purchase_number = $request['purchase_number'];
      $Purchase->purchase_date = $date;
      $Purchase->buyer = $request['buyer'];
      $Purchase->object = $request['object'];
      $Purchase->salesman = $request['salesman'];
      $Purchase->created_by = Auth::id();
      $Purchase->order = 'temp'; //Adds Temp order number
      $Purchase->save();

      //Updates Order number to the right one.
      $purchaseUpdate = Purchase::where('id', $Purchase->id)->first();
      $purchaseUpdate->order = $year.'P'.sprintf("%03d", $Purchase->id).'/'.$request['order'];
      $purchaseUpdate->update();

P.S如果我将Order(temp)设置为null,可能会是最好的吗?

为什么我需要这个?那么列表编号必须与订单编号相同。

例如:

list == 1

订单== 18P00 1 / X.

0 个答案:

没有答案