Php 总共有2个型号
我有一个名为Php 总共有2个型号,php,laravel,laravel-5,eloquent,laravel-5.8,Php,Laravel,Laravel 5,Eloquent,Laravel 5.8,我有一个名为seanse的表,有3个字段(id、type\u-seanse、price) 然后,我还有一个名为payments的表,其中有4个字段(id、fk\u type\u seance、number\u seance、total) 当,我在我的表格付款中添加记录 如果我选择了座位的类型理论上的(价格为de 100$),并且我输入了2个与座位相同的数字,则总数为200$ 我的控制器付款有问题,如何调整我的现场价格和fk_类型??要检索总数 $data['total'] = $reque
seanse
的表,有3个字段(id、type\u-seanse、price)
然后,我还有一个名为payments
的表,其中有4个字段(id、fk\u type\u seance、number\u seance、total)
当,我在我的表格付款中添加记录
如果我选择了座位的类型理论上的
(价格为de 100$),并且我输入了2个与座位相同的数字,则总数为200$
我的控制器付款有问题,如何调整我的现场价格和fk_类型??要检索总数
$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;
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
在计算之前,你必须得到你的座位。。。 大概是这样的:
$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
问题从这里开始:
$data['total'] = $request->price->fk_type_seance * $request->number_seance;
在这里,您根据$request->price->fk_type\u seance
和$request->number\u seamce
的乘积计算总计
,但在请求验证中,您没有价格
变量。。即使是这样,用一个数量乘以id(fk\u type\u seance
)也没有任何意义。你应该乘以价格和数量
因此,将其替换为:
// first find your seance instance:
$seance = Seance::find($request->fk_type_seance);
// then calculate the total
$data['total'] = $seance->price * $request->number_seance;
然后得到正确的总额
金额
此外,还应该对请求负载进行额外的验证。这里我添加了一个验证来检查fk\u type\u seance
在seanse
表中是否有匹配的id,但首先要确保它是一个整数。另外,您如何接收总计,在视图中计算它?无论如何,它应该是这样的:
$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
现在的函数如下所示。请注意,我使用而不是->all()
仅获取所需的数据(出于安全原因):
问题从这里开始:
$data['total'] = $request->price->fk_type_seance * $request->number_seance;
在这里,您根据$request->price->fk_type\u seance
和$request->number\u seamce
的乘积计算总计
,但在请求验证中,您没有价格
变量。。即使是这样,用一个数量乘以id(fk\u type\u seance
)也没有任何意义。你应该乘以价格和数量
因此,将其替换为:
// first find your seance instance:
$seance = Seance::find($request->fk_type_seance);
// then calculate the total
$data['total'] = $seance->price * $request->number_seance;
然后得到正确的总额
金额
此外,还应该对请求负载进行额外的验证。这里我添加了一个验证来检查fk\u type\u seance
在seanse
表中是否有匹配的id,但首先要确保它是一个整数。另外,您如何接收总计,在视图中计算它?无论如何,它应该是这样的:
$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;
$request->validate([
'fk_type_seance' => 'required|integer|exists:seances,id',
'number_seance' => 'required|integer',
'total' => 'required|numeric',
]);
现在的函数如下所示。请注意,我使用而不是->all()
仅获取所需的数据(出于安全原因):
我已经试过了,但我总是坚持。。。你有什么想法吗<代码>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')->('success','newdata created successfully');}
我已经编辑了我的第一条消息您正在直接覆盖$seance。这没有任何意义…删除$seance=$request->all();然后付款::创建([]);使用所需的字段…我已经尝试过了,但我总是坚持…如果(!$exists),您有什么想法吗{$seance=seance::where('id','=',$request->get('fk_-type_-seance'))->first();$seance=$request->all();if($seance)$total=$seance->price*$request->number\u-seance;Payment::create($seance);return redirect()->route('payments.index')->with('success','newdata created successfully');}
我已经编辑了我的第一条消息您正在直接覆盖$seance。这没有任何意义…删除$seance=$request->all();然后付款::创建([]);有了所需的字段…非常感谢HCK的解释。我的问题解决了,谢谢!^@user11124425很高兴能提供帮助。祝您愉快。另外@user11124425我做了一些小改动来改进代码。请仔细看一下。我想?$seance=seance::find($data['fk_-type_-seance');$data['total']=$seance->price*$data['number\u-seance'];
是的,你可以看到它。非常感谢HCK的解释。我的问题解决了,谢谢你!^^@user11124425很高兴能帮上忙。祝你愉快。另外@user11124425我做了一些小改动来改进代码。仔细看看它们。在这里,我想?$seance=seance::find($data['fk_type_seance'];$data['total']=$seance->price*$data['number_seance'];
是的,你可以看到它