php - 总共有2个模型
问题描述
我有一个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');
}
}
解决方案
在计算之前,您必须先进行降神会......这样的事情:
$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;
推荐阅读
- java - 如何在java中向特定客户端发送响应?
- angular - Angular:将用户活动发送到服务器
- reactjs - 带钩子的 React-cookies
- gcc - 无法修改数据段寄存器。尝试时抛出一般保护错误
- javascript - 为两个按钮添加一个条件,当其中一个被显示时点击它?
- javascript - 为什么在 vue.js 中这些数据不能正确地从父组件传递到子组件?
- r - 当替换返回表达式时如何获取函数名称
- python - 如何使用请求库在 Python 中将密钥发送到网页?
- android - 无法再运行我的应用程序说“INSTALLL_FAILED_VERIFICATION_FAILURE
- xsd - 缺少导致模式异常的 targetNamespace