【发布时间】:2019-05-04 20:01:11
【问题描述】:
如何将feeds表中具有相同cycle_id、type和user_id的数量相加,并将数量、cycle_id、type和user_id之和传递给inventories表?
feeds表
$table->increments('id');
$table->date('date_input');
$table->string('delivery_number');
$table->string('type');
$table->integer('quantity');
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
inventories表
$table->increments('id');
$table->string('type');
$table->integer('overall_quantity');
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
数量之和将传递给整体数量。feeds 表中的类型将传递给 inventories 表中的类型。feeds 表中的cycle_id 将传递给inventories 表中的cycle_id。feeds 表中的 user_id 将传递给 inventories 表中的 user_id。
如果库存表中不存在该条目,它将创建,但当 inventories 表中存在该条目时,它将添加。
这是我的代码
FeedController.php
public function store(Request $request)
{
//validate
$this->validate($request, array(
'date_input' => 'required|date',
'delivery_number' => 'required|numeric',
'type' => 'required|max:255',
'quantity' => 'required|numeric'
) );
$input= Carbon::parse($request->get('date_input'));
$cycle = Cycle::where('date_of_loading','<=',$input)
->where('date_of_harvest','>=',$input)
->first();
$cycle_id =$cycle->id ?? 0;
$feed = Feed::create([
'date_input' => request('date_input'),
'delivery_number' => request('delivery_number'),
'type' => request('type'),
'quantity' => request('quantity'),
'cycle_id' => $cycle_id,
'user_id' => Auth::id()
]);
return $feed;
$overall_quantity = Feed::where('cycle_id','=',$cycle_id)
->where('type','=',$request->get('type'))
->sum('quantity');
Inventory::firstOrCreate([
'overall_quantity' => $overall_quantity,
'type' => request('type'),
'cycle_id' => $cycle_id,
]);
}
但是没用
当我在feeds 表中添加数据时,inventories 表仍然是空的。
新问题
我的提要历史记录
我的inventories 表
它应该是 id 1 将是 144 但它会创建新条目。请帮助
【问题讨论】:
标签: php database laravel eloquent laravel-query-builder