我想通过使用inventory
表中的feed_consumed从consumption
表中减少_remain_overall_quantity
状况良好
如果$chickenAge
如果$chickenAge
> 8 && $chickenAge
如果$chickenAge
> 20 BFP中将发生递减
这是我的ConsumptionController.php
public function store(Request $request)
{
$this->validate($request, array(
'date_input' => 'required|date',
'feed_consumed' => 'required|numeric',
) );
//declaring values
$chickenAge = 0;
$input= Carbon::parse($request->get('date_input'));
$cycle = Cycle::where('date_of_loading','<=',$input)
->where('date_of_harvest','>=',$input)
->first();
if ($cycle) {
$start = Carbon::parse($cycle->date_of_loading);
$chickenAge = $start->diffInDays($input) ;
}
$inventory = Inventory::where('cycle_id','=',$cycle->id ?? 0)
->first();
$consumption = Consumption::create([
'date_input' => request('date_input'),
'chicken_age' => $chickenAge,
'feed_consumed' => request('feed_consumed'),
'cycle_id' => $cycle->id ?? 0,
'user_id' => Auth::id()
]);
return $consumption;
}
我不知道我可以将减量放在哪里,我也不知道if条件语句将是什么。你能帮助我吗?
最佳答案
尝试在return
语句之前添加以下代码:
if ($chickenAge <= 8)
{
DB::table('inventory')->where('id', 1)->decrement('remain_overall_quantity ', 1); // CBC
}
elseif ($chickenAge <= 20)
{
DB::table('inventory')->where('id', 2)->decrement('remain_overall_quantity ', 1); // BSC
}
else
{
DB::table('inventory')->where('id', 3)->decrement('remain_overall_quantity ', 1); // BFP
}
如果需要递减多于1,则在
'remain_overall_quantity ', 1
部分中更改“ 1”Laravel增减的一些见解:
https://laravel.com/docs/5.7/queries#increment-and-decrement