我有一个名为seances
的表,其中包含3个字段(id,type_seance,price)
然后,我还有一个名为payments
的表,其中包含4个字段(id,fk_type_seance,number_seance,total)
何时,我以Payment
格式添加录音
如果,我选择集会Theoretical
的类型(价格为100美元),并且输入2个类似集会的数量,则总计为200美元。
我的控制者付款有问题,如何调整我的现场价格和fk_type_seance?要检索总数?
$data['total'] = $request->price->fk_type_seance * $request->number_seance;
这是我的代码:
public function store(Request $request)
{
$request->validate([
'fk_type_seance' => 'required',
'number_seance' => 'required',
'total' => 'required'
]);
$exists = Payment
::where('fk_type_seance', $request->get('fk_type_seance'))
->where('number_seance', $request->get('number_seance'))
->where('total', $request->get('total'))
->count();
if (!$exists)
{
$data = $request->all();
$data['total'] = $request->price->fk_type_seance * $request->number_seance;
//Payment::create($request->all());
Payment::create($data);
return redirect()->route('payments.index')
->with('success', 'new data created successfully');
}
else
{
return redirect()->route('payments.index')
->with('error', 'doublon');
}
}
编辑代码jtwes
public function store(Request $request)
{
$request->validate([
'fk_type_seance' => 'required',
'number_seance' => 'required',
'total' => 'required'
]);
$exists = Payment::where('fk_type_seance', $request->get('fk_type_seance'))->where('number_seance', $request->get('number_seance'))->where('total', $request->get('total'))->count();
if (!$exists){
$seance = Seance
::where('id','=',$request->get('fk_type_seance'))
->first();
$seance = $request->all();
if ($seance) $total = $seance->price * $request->number_seance;
Payment::create($seance);
return redirect()->route('payments.index')
->with('success', 'new data created successfully');
}
else{
return redirect()->route('payments.index')
->with('error', 'doublon');
}
}
答案 0 :(得分:1)
在计算之前,您必须先保持冷静... 像这样:
$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;
答案 1 :(得分:1)
问题出在这里:
$data['total'] = $request->price->fk_type_seance * $request->number_seance;
在这里,您基于total
和$request->price->fk_type_seance
的乘积来计算$request->number_seamce
,但是在您的请求验证中,您没有price
变量。即使是这样,将数量乘以id(fk_type_seance
也没有意义。您应该乘以price
和数量。
因此,将其替换为:
// first find your seance instance:
$seance = Seance::find($request->fk_type_seance);
// then calculate the total
$data['total'] = $seance->price * $request->number_seance;
然后您将获得正确的total
金额。
此外,您还应该对请求有效负载进行其他验证。在这里,我要添加一个exists验证来检查fk_type_seance
表中seances
是否具有匹配的ID,但首先要确保它是一个integer
。另外,如何在视图中计算总数?无论如何,它应该看起来像这样:
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
因此,您现在的功能如下。请注意,出于安全原因,我使用$request->only(...)
method而不是->all()
来获取所需的数据:
public function store(Request $request)
{
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
$data = $request->only(['fk_type_seance', 'number_seance', 'total']);
$exists = Payment
::where('fk_type_seance', $data['fk_type_seance'])
->where('number_seance', $data['number_seance'])
->where('total', $data['total'])
->count();
if ( ! $exists)
{
$seance = Seance::find($data['fk_type_seance']);
$data['total'] = $seance->price * $data['number_seance'];
Payment::create($data);
return redirect()
->route('payments.index')
->with('success', 'new data created successfully');
}
else
{
return redirect()
->route('payments.index')
->with('error', 'doublon');
}
}