我正在开发一个POS,我注意到当我重新加载页面时,它会重新输入数据。如何防止数据重新进入页面重新加载?
我的控制器有以下代码:
public function DoAddSales(Request $request)
{
$rules = [
'customer_name'=>'required|alpha',
'or_number'=>'required',
'payment'=>'required',
];
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return back()->withErrors($validator);
} else {
foreach ($request->input('id_item') as $id) {
$item_id[] = $id;
}
foreach ($request->input('i_name') as $name) {
$i_name[] = $name;
}
foreach ($request->input('id_categ') as $categ) {
$categ_id[] = $categ;
}
foreach ($request->input('quan') as $quan) {
$quantity[] = $quan;
}
foreach ($request->input('tot') as $total) {
$each_total[] = $total;
}
$last = sizeof($request->input('id_item'));
$i = 0;
while ($i < $last) {
$items = Item::find($item_id[$i]);
$sales = new sales;
$sales->or = $request->input('or_number');
$sales->customer_name = $request->input('customer_name');
$sales->item_id = $item_id[$i];
$sales->item_name = $i_name[$i];
$sales->category_id = $categ_id[$i];
$sales->user_id = Auth::user()->id;
$sales->qty = $quantity[$i];
$sales->total = $each_total[$i];
$sales->save();
DB::table('items')->where('id',$item_id[$i])
->update(['qty'=> $items->qty - $quantity[$i]]);
return view ('show_sales')->with(array(
'name'=>$request->input('customer_name'),
'or_no'=>$request->input('or_number'),
'total'=>$request->input('getItemPrice'),
'change'=>$request->input('change'),
'orders'=>$i_name[$i],
));
$i++;
}
}
}
答案 0 :(得分:0)
使用重定向,如下所示。
假设重定向路由如\view\list
,那么您可以在函数末尾添加以下行
public function DoAddSales(Request $request){
...
// Your code
...
return redirect()->to('\view\list')->with('alert-success','Records saved Successfully');
}
注意:此重定向路由必须为GET
类型。