(Laravel) 如何对整数列求和并发送到另一个table?
(Laravel) How to total the integer column and send to the another table?
如何从 feeds
table 中用相同的 cycle_id、类型和 user_id 求和数量,cycle_id , 类型和 user_id 将传递给 inventories
table?
feeds
table
$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
$table->increments('id');
$table->string('type');
$table->integer('overall_quantity');
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
数量总和将传递给overall_quantity。
来自 feeds
table 的类型将传递给来自 inventories
table 的类型。
来自 feeds
table 的 cycle_id 将从 inventories
table.
传递到 cycle_id
来自 feeds
table 的 user_id 将从 inventories
table.
传递到 user_id
如果条目不存在于库存 table 中,它将创建,但当条目存在于 inventories
table 中时,它将添加。
这是我的代码
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
table 中添加数据时, inventories
table 仍然是空的。
新问题
我的 Feed 历史记录
我的inventories
table
它应该是 id 1 将是 144 但它创建了新条目。请帮助
在您 运行 您返回 Inventory::firstOrCreate([...])
命令之前 $feed
。
只需删除 return
语句。
// return $feed;
$overall_quantity = Feed::where('cycle_id','=',$cycle_id)
->where('type','=',$request->get('type'))
->sum('quantity');
Inventory::firstOrCreate([
//Add unique field combo to match here
'type' => request('type'),
'cycle_id' => $cycle_id,
],[ 'overall_quantity' => $overall_quantity ]);
如何从 feeds
table 中用相同的 cycle_id、类型和 user_id 求和数量,cycle_id , 类型和 user_id 将传递给 inventories
table?
feeds
table
$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
$table->increments('id');
$table->string('type');
$table->integer('overall_quantity');
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
数量总和将传递给overall_quantity。
来自 feeds
table 的类型将传递给来自 inventories
table 的类型。
来自 feeds
table 的 cycle_id 将从 inventories
table.
传递到 cycle_id
来自 feeds
table 的 user_id 将从 inventories
table.
如果条目不存在于库存 table 中,它将创建,但当条目存在于 inventories
table 中时,它将添加。
这是我的代码
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
table 中添加数据时, inventories
table 仍然是空的。
新问题
我的 Feed 历史记录
我的inventories
table
它应该是 id 1 将是 144 但它创建了新条目。请帮助
在您 运行 您返回 Inventory::firstOrCreate([...])
命令之前 $feed
。
只需删除 return
语句。
// return $feed;
$overall_quantity = Feed::where('cycle_id','=',$cycle_id)
->where('type','=',$request->get('type'))
->sum('quantity');
Inventory::firstOrCreate([
//Add unique field combo to match here
'type' => request('type'),
'cycle_id' => $cycle_id,
],[ 'overall_quantity' => $overall_quantity ]);