尽管我在Laravel中使用PUT,但不支持GET

如何解决尽管我在Laravel中使用PUT,但不支持GET

我正在使用Laravel 7,可以添加条目并从数据库中查看它们。当我尝试编辑或更新编辑的更改时,我会从Laravel收到警告,说GET method is not supported for this route. Supported methods: PUT。但是,我在web.php路由以及方法调用中都使用了PUT。我当然做错了。 这是我的路线称为工匠路线的视图:列表

Route:list

在我的路由组中的web.php中,这是我正在调用的控制器:

Route::group(['middleware' => ['auth','isAdmin']],function () {
    Route::get('/dashboard',function () {
        return view('admin.dashboard');
    });

    Route::get('registered-user','Admin\RegisteredController@index');
    Route::get('registered-empresa','Admin\EmpresaController@index');
    Route::get('role-edit/{id}','Admin\RegisteredController@edit');
    Route::put('role-update/{id}','Admin\RegisteredController@updaterole');
    Route::post('save-empresa','Admin\EmpresaController@store');
    Route::put('edit-empresa/{id}','Admin\EmpresaController@update');
});

这是我在EmpresaController.php中创建的更新函数:

 public function update(Request $request,$id)
    {
        $this->validate($request,[
            'erfc' => 'required','enombre' => 'required','ecalle' => 'required','ecolonia' => 'required','eciudad' => 'required','eestado' => 'required','ecpostal' => 'required','epais' => 'required',]);

        $empr = Empresa::find($id);

        $empr->erfc = $request->input('erfc');
        $empr->enombre = $request->input('enombre');
        $empr->ecalle = $request->input('ecalle');
        $empr->ecolonia = $request->input('ecolonia');
        $empr->eciudad = $request->input('eciudad');
        $empr->eestado = $request->input('eestado');
        $empr->ecpostal = $request->input('ecpostal');
        $empr->epais = $request->input('epais');

        $empr->update();

        return redirect('/registered-empresa')->with('status','Empresa se actualizó correctamente.');
    }

最后,这是我的empresas表的位置,我在其中都可以在index.blade.php文件中添加,查看和更新​​表:

@extends('layouts.admin')


@section('content')
<div class="container-fluid mt-5">

    <!-- Heading -->
    <div class="card mb-4 wow fadeIn">

      <!--Card content-->
      <div class="card-body d-sm-flex justify-content-between">

        <h4 class="mb-2 mb-sm-0 pt-1">
          <a href="/">Home Page</a>
          <span>/</span>
          <span>Empresas Registradas</span>
        </h4>
        @if (session('status'))
            <div class="alert alert-success" role="alert">
                {{ session('status') }}
            </div>
        @endif
        <div class="modal fade" id="modalRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                <div class="modal-header text-center">
                    <h4 class="modal-title w-100 font-weight-bold">Añadir Empresa</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="/save-empresa" method="POST">
                    {{ csrf_field() }}
                <div class="modal-body mx-3">
                    <div class="md-form mb-1">
                        <input type="text" name="erfc" id="orangeForm-erfc" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-erfc">RFC</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="enombre" id="orangeForm-enombre" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-enombre">Nombre</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="ecalle" id="orangeForm-ecalle" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-ecalle">Calle</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="ecolonia" id="orangeForm-ecolonia" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-ecolonia">Colonia</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="eciudad" id="orangeForm-eciudad" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-eciudad">Ciudad</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="eestado" id="orangeForm-eestado" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-eestado">Estado</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="ecpostal" id="orangeForm-ecpostal" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-ecpostal">Codigo Postal</label>
                    </div>
                    <div class="md-form mb-1">
                        <input type="text" name="epais" id="orangeForm-epais" class="form-control validate">
                        <label data-error="wrong" data-success="right" for="orangeForm-epais">País</label>
                    </div>
                    <div style="display: none;" class="md-form mb-1">
                    <input type="text" name="euser" readonly id="orangeForm-euser" class="form-control validate" value="{{ Auth::user()->id }}">

                    </div>
                    <div style="display: none;" class="md-form mb-1">
                        <input type="text" name="eregby" readonly id="orangeForm-eregby" class="form-control validate" value="{{ Auth::user()->id }}">

                    </div>
                </div>
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-deep-orange">Añadir</button>
                </div>
            </form>
                </div>
            </div>
        </div>
<div class="text-center">
  <a href="" class="btn btn-default btn-rounded mb-4" data-toggle="modal" data-target="#modalRegisterForm"><i class="fa fa-plus" aria-hidden="true"></i>&nbsp;&nbsp; Add</a>
</div>


<!--edit modal start-->

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header text-center">
            <h4 class="modal-title w-100 font-weight-bold">Editar Empresa</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <form action="edit-empresa/" id="editForm">
            {{ csrf_field() }}
            @method('PUT')
                <div class="modal-body mx-3">
                    <div class="md-form mb-1">
                        <input placeholder="RFC" type="text" name="erfc" id="erfc" class="form-control validate">
                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Nombre" type="text" name="enombre" id="enombre" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Calle" type="text" name="ecalle" id="ecalle" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Colonia" type="text" name="ecolonia" id="ecolonia" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Ciudad" type="text" name="eciudad" id="eciudad" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Estado" type="text" name="eestado" id="eestado" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="Codigo Postal" type="text" name="ecpostal" id="ecpostal" class="form-control validate">

                    </div>
                    <div class="md-form mb-1">
                        <input placeholder="País" type="text" name="epais" id="epais" class="form-control validate">
                    </div>
                    <div style="display: none;" class="md-form mb-1">
                        <input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
                    </div>
                    <div style="display: none;" class="md-form mb-1">
                        <input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
                    </div>
                </div>
                {{-- <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-deep-orange">Editar</button>
                </div> --}}
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-deep-orange">Editar</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!--end edit modal-->

      </div>

    </div>
    <!-- Heading -->

    <!--Grid row-->
      <!--Grid column-->
      <div class="row">
        <!--Card-->
        <div class="col-md-12 mb-4">
          <!--Card content-->
          <div class="card">
            <!-- List group links -->
             <div class="card-body">

                <table id="datatable2" class="table table-bordered">
                    <thead>
                        <tr>
                           <th>RFC</th>
                           <th>Nombre</th>
                           <th>Calle</th>
                           <th>Colonia</th>
                           <th>Ciudad</th>
                           <th>Estado</th>
                           <th>Codigo Postal</th>
                           <th>País</th>
                           <th>Acción</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($empresas as $empresa)
                         <tr>
                         <td>{{ $empresa->erfc }}</td>
                         <td>{{ $empresa->enombre }}</td>
                         <td>{{ $empresa->ecalle }}</td>
                         <td>{{ $empresa->ecolonia }}</td>
                         <td>{{ $empresa->eciudad }}</td>
                         <td>{{ $empresa->eestado }}</td>
                         <td>{{ $empresa->ecpostal }}</td>
                         <td>{{ $empresa->epais }}</td>

                         <td>
                            <div class="text-center">
                                <a href="" class="badge badge-pill btn-primary px-3 py-2 edit" data-toggle="modal" data-target="#editModal">Editar</a>

                            <a class="badge badge-pill btn-danger px-3 py-2" href="">Borrar</a>
                        </div>
                         </td>
                         </tr>
                        @endforeach
                    </tbody>
                </table>

            </div>
            <!-- List group links -->
          </div>
        </div>
        <!--/.Card-->
      </div>
      <!--Grid row-->
  </div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
    let table = $('#datatable2').DataTable();

    // Start edit record
        table.on('click','.edit',function() {
            $tr = $(this).closest('tr');
            if($($tr).hasClass('child')) {
                $tr = $tr.prev('.parent');
            }

            let data = table.row($tr).data();
            console.log(data);

            $('#erfc').val(data[0]);
            $('#enombre').val(data[1]);
            $('#ecalle').val(data[2]);
            $('#ecolonia').val(data[3]);
            $('#eciudad').val(data[4]);
            $('#eestado').val(data[5]);
            $('#ecpostal').val(data[6]);
            $('#epais').val(data[7]);

            $('#editForm').attr('action','/edit-empresa/'+data[0]);
            $('#editModal').modal('show');
        });
    // End edit record
});
</script>
@endsection

我很确定我在此文件中做错了什么。如果我对如何更好地做到这一点有所帮助,或者如果我错过了什么,我一定会很感激。预先谢谢你。

解决方法

即使您将POST包含在表单中,在定义表单时也需要将方法指定为@method('PUT')。这是因为HTML不直接支持PUT方法,默认情况下它将是GET。所以要纠正:

更改此:

<form action="edit-empresa/" id="editForm">

TO

<form action="edit-empresa/" id="editForm" method="POST">
,

在web.php中

Route::patch('edit-empresa/{id}','Admin\EmpresaController@update');

index.blade.php

@method('PATCH')
,

由于某种原因,我无法使用modal和jquery方法使它正常工作,因此我从index.blade.php的底部删除了数据表jquery。我的第一个错误是不首先调用数据。我在view-admin-empresa文件夹中创建了一个名为edit.blade.php的单独文件。这是代码:

@extends('layouts.admin')


@section('content')
<div class="container-fluid mt-5">

<!-- Heading -->
<div class="card mb-4 wow fadeIn">

    <!--Card content-->
    <div class="card-body d-sm-flex justify-content-between">

      <h4 class="mb-2 mb-sm-0 pt-1">
        <span>Empresa Registrada - Editar Empresa</span>
      </h4>
    </div>

  </div>
  <!-- Heading -->
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">Editar Empresa</h4>
            <form action="{{ url('empresa-update/'.$empresa->id) }}" id="editForm" method="POST">
                    {{ csrf_field() }}
                    {{ method_field('PUT') }}
                        <div class="modal-body mx-3">
                            <div class="md-form mb-1">
                            <label for="erfc">RFC</label>
                            <input value="{{ $empresa->erfc }}" type="text" name="erfc" id="erfc" class="form-control validate">
                            </div>
                            <div class="md-form mb-1">
                                <label for="enombre">Nombre</label>
                                <input value="{{ $empresa->enombre }}" type="text" name="enombre" id="enombre" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="ecalle">Calle</label>
                                <input value="{{ $empresa->ecalle }}" type="text" name="ecalle" id="ecalle" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="ecolonia">Colonia</label>
                                <input value="{{ $empresa->ecolonia }}" type="text" name="ecolonia" id="ecolonia" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="ecuidad">Ciudad</label>
                                <input value="{{ $empresa->eciudad }}" type="text" name="eciudad" id="eciudad" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="eestado">Estado</label>
                                <input value="{{ $empresa->eestado }}" type="text" name="eestado" id="eestado" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="ecpostal">Codigo Postal</label>
                                <input value="{{ $empresa->ecpostal }}" type="text" name="ecpostal" id="ecpostal" class="form-control validate">

                            </div>
                            <div class="md-form mb-1">
                                <label for="epais">País</label>
                                <input value="{{ $empresa->epais }}" type="text" name="epais" id="epais" class="form-control validate">
                            </div>
                            <div style="display: none;" class="md-form mb-1">
                                <input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
                            </div>
                            <div style="display: none;" class="md-form mb-1">
                                <input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
                            </div>
                        </div>

                        <div class="modal-footer d-flex justify-content-center">
                        <a href="{{ url('registered-empresa') }}" class="btn btn-secondary">Cancelar</a>
                            <button type="submit" class="btn btn-deep-orange">Editar</button>
                        </div>
                    </form>
            </div>
        </div>
    </div>
</div>
</div>
@endsection

然后在web.php中,我创建了以下路由:

Route::group(['middleware' => ['auth','isAdmin']],function () {
    Route::get('/dashboard',function () {
        return view('admin.dashboard');
    });

    Route::get('registered-user','Admin\RegisteredController@index');
    Route::get('registered-empresa','Admin\EmpresaController@index');
    Route::get('role-edit/{id}','Admin\RegisteredController@edit');
    Route::put('role-update/{id}','Admin\RegisteredController@updaterole');
    Route::post('save-empresa','Admin\EmpresaController@store');
    Route::get('/edit-empresa/{id}','Admin\EmpresaController@edit');
    Route::put('/empresa-update/{id}','Admin\EmpresaController@update');
});

如前所述,我取消了编辑模式,并将其重定向到empresa edit.blade.php文件。 现在,我可以毫无问题地进行编辑了。感谢 Arjun bhati user3532758 在此问题上有所作为。我真的很感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-