我的HTML表单定义如下:
<form action="{{route('save.checkout')}}" method="POST">
{{csrf_field()}}
<input name="amount" type="hidden" value="{{session()->get('cart')->totalprice}}">
<div class="cart_navigation">
<a class="continue-btn" href="#">
<i class="fa fa-arrow-left"> </i> خرید را ادامه دهید
</a>
<a class="checkout-btn" href="{{route('save.checkout')}}">
<i class="fa fa-check"></i> ادامه به پرداخت
</a>
</div>
</form>
提交表单时抛出以下错误:
The GET method is not supported for this route. Supported methods: POST.
我的路线是:
route::post('/savecheckout','BasketController@checkout')->name('save.checkout');
和结帐功能:
public function checkout(Request $request){
$user = auth()->user()->id;
$order = new order();
$order->user_id = $user;
$order->amount = $request->input('amount');
$order->status = 0;
$order->save();
$order = order::where('status' ,0)->where('user_id', $user)->first();
return view('checkout.index', compact('order'));
}
答案 0 :(得分:0)
答案 1 :(得分:0)
如果您是从Laravel开始的,那么我想为将来的项目做出一些贡献,以使您的编程更好,更安全。
所以修改后的代码如下:
public function checkout(Request $request){
// use Validator; (add this after namespace to import validator)
$validator = Validator::make($request->all(),[
'user_id' => 'required|integer|max:11',
'amount' => 'required|numeric',
'status' => 'sometimes|integer|max:1',
]);
if($validator->fails()) {
return back()->withErrors($validator);
}
try {
$user = auth()->user()->id;
$order = new order();
$order->user_id = $user;
$order->amount = $request->input('amount');
$order->status = 0;
$order->save();
$request->session()->flash('message', 'Order Successfully Created');
$order = order::where('status' ,0)->where('user_id', $user)->first();
return view('checkout.index', compact('order'));
} catch (\Exception $e){
dd($e->getMessage()); // it will show the error message with, you can replace this block with redirect code or anything else..
}
}
用于在结帐> index.blade.php模板中的代码下面的前端使用中显示错误和成功消息(仅是示例代码,您可以使用自己的CSS和样式使其更出色)
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
@if(count($errors) > 0)
<div class="note note-error">
<h4>Error..!!</h4>
<p>
@foreach($errors->all() as $error)
<div class="alert alert-danger fade in m-b-15">
<i class="fa fa-chevron-right"></i>
{{ $error }}
<span class="close" data-dismiss="alert">×</span></div>
@endforeach
</p>
</div>
@endif