【发布时间】:2019-05-04 09:11:36
【问题描述】:
我想通过选择表中的type和cycle_id 列来获取feeds 表中的数量总和。之后,inventory 表将得到总和并保存。
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();
return Feed::create([
'date_input' => request('date_input'),
'delivery_number' => request('delivery_number'),
'type' => request('type'),
'quantity' => request('quantity'),
'cycle_id' => $cycle->id ?? 0,
'user_id' => Auth::id()
]);
}
使用type和cycle_id,用户将获得总量并将其发送到inventory表。
$overall_quantity = Feed::where('cycle_id' '=' $cycle->id ?? 0)
->where('type' '=' $request->get('type'))
->sum('quantity');
如果inventory 表中不存在总量,它将创建一个新列,如果存在总量,它将添加到现有总量中。
库存表
$table->increments('id');
$table->string('type');
$table->integer('overall_quantity);
$table->unsignedInteger('cycle_id');
$table->unsignedInteger('user_id');
你能帮帮我吗?谢谢
【问题讨论】:
-
如果不存在则创建新记录,否则更新它对吗?
-
是的,先生,它会更新
-
使用
return Feed::firstOrNew([而不是return Feed::create([ -
库存表怎么样?
-
您希望它在
Feed或inventory表中创建/更新?
标签: php mysql database laravel laravel-query-builder