2016-08-22 13 views
0

我有一張表格,我從一張表格插入一批輸入。這是因爲每個條目共享一個唯一的產品代碼,但每個條目都有唯一的序列號。用戶插入批次的數量,可以說5,並且該項目被保存5次。控制器看起來是這樣的:按順序粘貼多個記錄的後綴 - laravel

public function store() 
    { 

      $data = Input::get(); 

      for ($i=0; $i<($data['quantity']); $i++){ 
      $this->repository->saveItem($data, ''); 


      $initial = Inventory::orderBy('created_at', 'desc')->first(); 
      $batch = Inventory::where('created_at', '=', $initial['created_at'])->get(); 



     return Redirect::route('inventory.index')->with('flash_notice', "Item Created"); 
    } 

我可以通過尋找匹配相同的created_at日期($批次)的所有記錄檢索整批。我想要做的是給每個記錄一個唯一的序列號,這是產品代碼加上一個數字(第一個入口獲得01,第二個02等)。

我的看法是這樣的形式:

{!! Form::open(array('method'=>'POST', 'route'=>array('inventory.store'))) !!} 

<div class="form-group"> 
        {!! Form::label('production_id', 'Production ID', ['class' => 'control-label']) !!} 
        {!! Form::text('production_id', null, ['class' => 'form-control']) !!} 
       </div> 
    <div class="form-group"> 
        {!! Form::label('quantity', 'Quantity of chips:', ['class' => 'control-label']) !!} 
        {!! Form::number('quantity', null, ['class' => 'form-control']) !!} 
       </div> 

       {!! Form::submit('Create New Batch Entry', ['class' => 'btn btn-primary']) !!} 
      {!! Form::close() !!} 

我的模式是:

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Inventory extends Model 
{ 
    use SoftDeletes; 

    /** 
    * @var string 
    */ 
    protected $table = "inventory"; 

    /** 
    * @var string 
    */ 
    protected $primaryKey = "id"; 

    /** 
    * @var array 
    */ 
    protected $fillable = array(

     'serial_number', 
     'production_id', 
     'created_at', 
     'deleted_at', 

    ); 
} 

我在想$我在$一批循環一個foreach粘貼後綴到$批['SERIAL_NUMBER ']將序列號從表單保存爲production_id時。我無法弄清楚如何在laravel中順序地粘貼一個後綴。

回答

1
$batch->each(function ($item, $index) use ($productcode) { 
    $item->update(['serial_number' => $productCode . $index]); 
}); 

在另一方面,它似乎並沒有超級準確使用「最新」 created_at日期,以確定來自同一批次的項目。 也許有一個批處理表,您首先創建一個新記錄,爲您提供'批處理ID',然後將該批處理ID添加到新庫存項目中。

+1

我同意這一點,有一個潛在的時間戳可能是微秒關閉和兩個批次的**非常苗條的可能性在同一時間戳一次。 創建一個名爲'batches'的表具有自己的唯一ID,然後附加一個'inventory_batches'表,其中將包含'batch_id'和'inventory_id'(數據透視表)。 –

+0

謝謝你的評論。我同意使用數據透視表。我感到困惑的是你如何生成你的索引粘貼到產品代碼。 – MRF

+0

我已經弄清楚如何獲得索引,不用擔心。 – MRF