laravel 此路由不支持 post 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55468295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The post method is not supported for this route
提问by eraufi
I'm new to laravel and I want to submit a form which have three attachments plus some arrays
我是 laravel 的新手,我想提交一个包含三个附件和一些数组的表单
but when I click submit button laravel says
但是当我点击提交按钮 laravel 说
"The POST method is not supported for this route. Supported methods: GET, HEAD."
“此路由不支持 POST 方法。支持的方法:GET、HEAD。”
I've searched a lot but couldn't find any errors
我搜索了很多但找不到任何错误
My View
我的看法
<div>
<form action="insertquotation" method="post" enctype="multipart/form-data">
@csrf
<table border="1" id="formTable">
<thead>
<tr>
<td>procurment_request_number</td>
<td>quotationer</td>
<td>shop_name</td>
<td>shop_account_number</td>
<td>shop_permit</td>
<td>shop_quatation</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="procurment_request_number" id="procurment_request_number" value="{{$result->first()->request_number}}">
{{--<select name="procurment_request_number[]" id="procurment_request_number">--}}
{{--@foreach($result as $results)--}}
{{--<option value="{{$results->request_number}}">{{$results->request_number}}</option>--}}
{{--@endforeach--}}
{{--</select>--}}
</td>
<td><input type="text" id="quotationer1" name="quotationer1"></td>
<td><input type="text" name="shop_name1" id="shop1_name"></td>
<td><input type="text" name="shop_account_number1" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit1" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation1" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer2"></td>
<td><input type="text" name="shop_name2" id="shop1_name"></td>
<td><input type="text" name="shop_account_number2" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit2" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation2" id="shop_quatation"></td>
</tr>
<tr>
<td></td>
<td><input type="text" id="quotationer" name="quotationer3"></td>
<td><input type="text" name="shop_name3" id="shop1_name"></td>
<td><input type="text" name="shop_account_number3" id="shop1_account_number"></td>
<td><input type="text" name="shop_permit3" id="shop1_permit"></td>
<td><input type="file" accept="application/pdf" name="shop_quatation3" id="shop_quatation"></td>
</tr>
<tr>
<td>Item_ID</td>
<td>Quantity</td>
<td>Unit_ID</td>
<td>description</td>
<td>Shop1_price</td>
<td>Shop2_price</td>
<td>Shop3_price</td>
</tr>
@foreach($result as $results)
<tr>
<td><input type="text" name="itemid[]" id="itemid" value="{{$results->id}}"></td>
<td><input type="text" name="quantity[]" id="quantity" value="{{$results->quantity}}"></td>
<td><input type="text" name="units_id[]" id="units_id" value="{{$results->units_id}}"></td>
<td><input type="text" name="description[]" id="description" value="{{$results->description}}"></td>
<td><input type="text" name="shop1_price[]" id="shop1_price"></td>
<td><input type="text" name="shop2_price[]" id="shop2_price"></td>
<td><input type="text" name="shop3_price[]" id="shop3_price"></td>
</tr>
</tbody>
@endforeach
</table>
{{--<input value="addrow" type="button" id="addrow" onclick="javascript: addRow();">--}}
<input type="submit" value="submit">
</form>
</div>
My Controller
我的控制器
public function insertquotation(request $request)
{
if ($file = $request->file('shop1_quatation')) {
$name = $file->getClientOriginalName();
$file->move('procurement_files', $name);
$file2 = $request->file('shop2_quatation');
$name2 = $file2->getClientOriginalName();
$file2->move('procurement_files', $name2);
$file3 = $request->file('shop2_quatation');
$name3 = $file2->getClientOriginalName();
$file3->move('procurement_files', $name3);
$procurment_request_number = $request->input('procurment_request_number');
$quotationer1 = $request->input('quotationer1');
$quotationer2 = $request->input('quotationer2');
$quotationer3 = $request->input('quotationer3');
$shop_name1 = $request->input('shop_name1');
$shop_account_number1 = $request->input('shop_account_number1');
$shop_permit1 = $request->input('shop_permit1');
$shop_name2 = $request->input('shop_name2');
$shop_account_number2 = $request->input('shop_account_number2');
$shop_permit2 = $request->input('shop_permit2');
$shop_name3 = $request->input('shop_name3');
$shop_account_number3 = $request->input('shop_account_number3');
$shop_permit3 = $request->input('shop_permit3');
$numbers = $request->input('itemid');
for ($i = 0; $i < count($numbers); $i++) {
$itemid = $request->input('itemid');
$shop1_price = $request->input('shop1_price');
$shop2_price = $request->input('shop2_price');
$shop3_price = $request->input('shop3_price');
DB::insert("INSERT INTO procurment_quotations (`procurment_request_number`, `itemid`, `quotationer1`, `quotationer2`, `quotationer3`, `shop1_name`, `shop1_account_number`, `shop1_permit`, `shop1_quatation`, `shop1_price`, `shop2_name`, `shop2_account_number`, `shop2_permit`, `shop2_quatation`, `shop2_price`, `shop3_name`, `shop3_account_number`, `shop3_permit`, `shop3_quatation`, `shop3_price`, `created_at`, `updated_at`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)", [$procurment_request_number, $itemid[$i],$quotationer1,$quotationer2,$quotationer3,$shop_name1,$shop_account_number1,$shop_permit1,$name,$shop1_price,$shop_name2,$shop_account_number2,$shop_permit2,$name2,$shop2_price,$shop_name3,$shop_account_number3,$shop_permit3,$name3,$shop3_price]);
}
}
}
and my route
还有我的路线
Route::Post('insertquotation','Home@insertquotation');
The error is
错误是
"The POST method is not supported for this route. Supported methods: GET, HEAD."
“此路由不支持 POST 方法。支持的方法:GET、HEAD。”
回答by
I think from action="insertquotation"
, you should make it into action="{{url('insertquotation')}}"
我想从action="insertquotation"
,你应该把它变成action="{{url('insertquotation')}}"
And may I see the codes in your routes/web.php
file? you should declare the route insertquotation
there
我可以看看你routes/web.php
文件中的代码吗?你应该在insertquotation
那里声明路线
For example:
例如:
Route::post('insertquotation', 'YourController@yourMethod');
回答by Udhav Sarvaiya
Change in your blade file:
更改您的刀片文件:
<form method="POST" enctype="multipart/form-data" action="{{ route('insertquotation') }}">
Change Post
to post
in your routes/web.phpfile:
在您的routes/web.php文件中更改Post
为:post
Route::post('insertquotation','Controllername@methodname')->name('insertquotation');
回答by Advaith
Couple of things which I need to point out on your code
我需要在您的代码中指出的几件事
It is always better to use route names while creating routes so that when you need to change the route URL to something else, you only need to change it inside routes file (not view files)
创建路由时最好使用路由名称,这样当您需要将路由 URL 更改为其他内容时,您只需在路由文件中更改它(而不是查看文件)
Route::post('insertquotation','Home@insertquotation')->name('insertquotation');
Now inside your view, you need to do this
现在在您的视图中,您需要执行此操作
<form method="POST" enctype="multipart/form-data" action="{{ route('insertquotation') }}"> ... </form>
In the future when you want (maybe not) to change the URL to /quotation/insert
, all you have to do is to change it inside the routes file.
将来当您想要(也许不)将 URL 更改为 时/quotation/insert
,您所要做的就是在路由文件中更改它。
Now, inside your controller,
现在,在您的控制器中,
There is a function to check whether the file exists $request->hasFile('shop1_quatation')
which more correct to use inside an if condition rather passes in the file.
有一个函数来检查文件是否存在$request->hasFile('shop1_quatation')
,哪个更正确地在 if 条件中使用而不是传入文件。
Also, it is much better to use Laravel eloquent for inserting data into the database. Learn more here
此外,最好使用 Laravel eloquent 将数据插入数据库。在此处了解更多信息