此路由不支持 post 方法

新手上路,请多包涵

我是 laravel 的新手,我想提交一个包含三个附件和一些数组的表单

但是当我点击提交按钮时,laravel 说

“此路由不支持 POST 方法。支持的方法:GET、HEAD。”

我搜索了很多但找不到任何错误

我的看法

<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>

我的控制器


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]);
        }
    }
}

和我的路线

Route::Post('insertquotation','Home@insertquotation');

错误是

“此路由不支持 POST 方法。支持的方法:GET、HEAD。”

原文由 eraufi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 370
2 个回答

routes/web.php 文件中为路由命名:

 Route::post('insertquotation','Controllername@methodname')
    ->name('insertquotation');

然后更改刀片文件中的表单操作以指向该路由:

 <form action="{{ route('insertquotation') }}" method="post" enctype="multipart/form-data">

原文由 Udhav Sarvaiya 发布,翻译遵循 CC BY-SA 4.0 许可协议

我需要在您的代码中指出几件事

在创建路由时最好使用路由名称,这样当您需要将路由 URL 更改为其他内容时,您只需在路由文件(而不是查看文件)中更改它

Route::post('insertquotation','Home@insertquotation')->name('insertquotation');

现在在你的视野中,你需要这样做

<form method="POST" enctype="multipart/form-data" action="{{ route('insertquotation') }}"> ... </form>

将来当您想要(也许不想)将 URL 更改为 /quotation/insert 时,您所要做的就是在路由文件中更改它。

现在,在你的控制器中,

有一个函数可以检查文件是否存在 $request->hasFile('shop1_quatation') 在 if 条件中使用更正确,而不是传入文件。

此外,最好使用 Laravel Eloquent 将数据插入数据库。 在这里了解更多

原文由 Advaith 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题