Php (Laravel)如何合计整数列并发送到另一个表?
如何对Php (Laravel)如何合计整数列并发送到另一个表?,php,database,laravel,eloquent,laravel-query-builder,Php,Database,Laravel,Eloquent,Laravel Query Builder,如何对提要表中具有相同周期id、类型和用户id的数量求和,并且数量、周期id、类型和用户id之和将传递给库存表 提要表格 $table->increments('id'); $table->date('date_input'); $table->string('delivery_number'); $table->string('type'); $table->integer('quantity'); $table
提要
表中具有相同周期id、类型和用户id的数量求和,并且数量、周期id、类型和用户id之和将传递给库存
表
提要
表格
$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();
存货
表
$table->increments('id');
$table->string('type');
$table->integer('overall_quantity');
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
$table->timestamps();
数量之和将传递到总数量。提要
表中的类型将传递到库存
表中的类型。来自
提要
表的循环id将传递到来自库存
表的循环id。来自
提要
表的用户id将从库存
表传递给用户id。如果库存表中不存在该条目,它将创建该条目,但当该条目存在于
库存表中时,它将添加该条目
这是我的密码
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,
]);
}
公共函数存储(请求$Request)
{
//证实
$this->validate($request,array)(
“日期输入”=>“所需日期”,
“交货编号”=>“必需”|数字”,
'类型'=>'必需|最大值:255',
“数量”=>“所需数量”|数字”
) );
$input=Carbon::parse($request->get('date_input'));
$cycle=cycle::其中('date\u of_loading','=',$input)
->第一个();
$cycle\u id=$cycle->id??0;
$feed=feed::create([
“日期输入”=>请求(“日期输入”),
“交货编号”=>请求(“交货编号”),
'type'=>请求('type'),
“数量”=>请求(“数量”),
“周期id”=>$cycle\u id,
'user_id'=>Auth::id()
]);
返回$feed;
$totall_quantity=Feed::where('cycle_id','=',$cycle_id)
->其中('type','=',$request->get('type'))
->总和(“数量”);
库存::firstOrCreate([
“总体数量”=>总体数量,
'type'=>请求('type'),
“周期id”=>$cycle\u id,
]);
}
但它不起作用
当我在提要
表中添加数据时,库存
表仍然为空
新问题
我的饲料历史
我的存货
表
$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();
它应该是id 1,但它将创建新条目。在运行返回的$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 ]);
在运行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 ]);
我有新问题我有新问题