按顺序将后缀粘贴到多个记录 - laravel
Pasting a suffix to multiple records sequentially - laravel
我有一个 table,我在其中插入了来自单个表单的一批输入。这是因为每个条目共享一个唯一的产品代码,但每个条目都有一个唯一的序列号。用户输入批次的数量,假设为 5,则该项目被保存 5 次。控制器看起来像这样:
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 日期($batch)的所有记录来检索整个批次。我想要做的是给每条记录一个唯一的序列号,这是产品代码加上一个数字(第一个条目得到 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 $i in $batch 循环,当序列号从表单中保存为 production_id 时,将后缀粘贴到 $batch['serial_number'] 上。我不知道如何在 laravel.
中按顺序粘贴后缀
$batch->each(function ($item, $index) use ($productcode) {
$item->update(['serial_number' => $productCode . $index]);
});
另一方面,使用 'latest' created_at 日期来识别同一批次的商品似乎不是很准确。
也许有一个批次 table,您首先创建一个新记录给您一个 'batch id',然后将该批次 ID 添加到您的新库存项目中。
我有一个 table,我在其中插入了来自单个表单的一批输入。这是因为每个条目共享一个唯一的产品代码,但每个条目都有一个唯一的序列号。用户输入批次的数量,假设为 5,则该项目被保存 5 次。控制器看起来像这样:
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 日期($batch)的所有记录来检索整个批次。我想要做的是给每条记录一个唯一的序列号,这是产品代码加上一个数字(第一个条目得到 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 $i in $batch 循环,当序列号从表单中保存为 production_id 时,将后缀粘贴到 $batch['serial_number'] 上。我不知道如何在 laravel.
中按顺序粘贴后缀$batch->each(function ($item, $index) use ($productcode) {
$item->update(['serial_number' => $productCode . $index]);
});
另一方面,使用 'latest' created_at 日期来识别同一批次的商品似乎不是很准确。 也许有一个批次 table,您首先创建一个新记录给您一个 'batch id',然后将该批次 ID 添加到您的新库存项目中。