Laravel 两个请求值递减并插入到列中
Laravel Two request value get decrement and insert in column
我是 Laravel 的新手,我的控制器试图减少(减去)两个输入请求值并将结果传递到 ( balance_amount ) 列。
例子
input price request 100
input paid_amount request 50
result balance_amount 50
我的控制器
public function pay(Request $request)
{
$request['price'] = $request->price; //PRICE 0
$request['paid_amount'] = $request->paid_amount; //PAID 0
$request['balance_amount'] = $request->balance_amount; //BALANCE
Installment::create($request);
}
return redirect( )->with('success',' PAID SUCCESSFULLY');
}
public function pay(Request $request)
{
$validated = $request->validate([
'price' => ['integer',],
'paid_amount' => ['integer',],
]);
Installment::create(
[
'price' => $validated->price,
'paid_amount' => $validated->paid_amount,
'balance_amount' => $validated->price - $validated->paid_amount,
]
);
return redirect()->with('success', ' PAID SUCCESSFULLY');
}
显然,您不应该根据请求创建模型,这样做没有意义,您不能使用新变量来存储要创建的数据;
public function pay(Request $request){
$sourceData = $request->only('price', 'paid_amount', 'balance_amount');
//you can modify the $souceData variable as you want to custom the data you need to insert
Installment::create($sourceData);
}
我是 Laravel 的新手,我的控制器试图减少(减去)两个输入请求值并将结果传递到 ( balance_amount ) 列。
例子
input price request 100
input paid_amount request 50
result balance_amount 50
我的控制器
public function pay(Request $request)
{
$request['price'] = $request->price; //PRICE 0
$request['paid_amount'] = $request->paid_amount; //PAID 0
$request['balance_amount'] = $request->balance_amount; //BALANCE
Installment::create($request);
}
return redirect( )->with('success',' PAID SUCCESSFULLY');
}
public function pay(Request $request)
{
$validated = $request->validate([
'price' => ['integer',],
'paid_amount' => ['integer',],
]);
Installment::create(
[
'price' => $validated->price,
'paid_amount' => $validated->paid_amount,
'balance_amount' => $validated->price - $validated->paid_amount,
]
);
return redirect()->with('success', ' PAID SUCCESSFULLY');
}
显然,您不应该根据请求创建模型,这样做没有意义,您不能使用新变量来存储要创建的数据;
public function pay(Request $request){
$sourceData = $request->only('price', 'paid_amount', 'balance_amount');
//you can modify the $souceData variable as you want to custom the data you need to insert
Installment::create($sourceData);
}