GET nicht unterstützt, obwohl ich PUT in Laravel verwende

Route:list

in meiner Route Group in web.php Hier sind die Controller, die ich anrufe:

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

    Route::get('registered-user', 'Admin\[email protected]');
    Route::get('registered-empresa', 'Admin\[email protected]');
    Route::get('role-edit/{id}', 'Admin\[email protected]');
    Route::put('role-update/{id}', 'Admin\[email protected]');
    Route::post('save-empresa', 'Admin\[email protected]');
    Route::put('edit-empresa/{id}', 'Admin\[email protected]');
});

Hier ist die Update-Funktion, die ich in EmpresaController.php erstellt habe:

 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.');
    }

Und schließlich, hier ist der Speicherort meiner empresas-Tabelle, wo ich beide meine Tabelle in meiner index.blade.php-Datei hinzufügen, anzeigen und aktualisieren:

@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

Ich bin mir ziemlich sicher, dass es in dieser Datei ist, dass ich etwas falsch mache. Jede Hilfe, wie ich dies besser machen kann oder wenn ich etwas verpasst habe, würde ich es sicherlich zu schätzen wissen. Vielen Dank im Voraus.

none none

Antwort auf "GET nicht unterstützt, obwohl ich PUT in Laravel verwende " 3 von antworten

Sie müssen die Methode als POST when defining the form even though you are including the angeben, wenn Sie das Formular definieren, obwohl Sie das @method('PUT') inside the form. That is because HTML does not support in das Formular einschließen. Das liegt daran, dass HTML die PUT method directly, and by default it will be a -Methode nicht direkt unterstützt und standardmäßig eine GET. So to correct: ist. Also korrigieren:

Ändern Sie dies:

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

TO

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

in web.php

Route::patch('edit-empresa/{id}', 'Admin\[email protected]');

index.blade.php

@method('PATCH')
Jetzt kann ich problemlos bearbeiten. Vielen Dank an Arjun bhati und user3532758 für einen Stich in dieses Problem. Ich schätze es sehr.

Aus irgendeinem Grund konnte ich dies nicht mit der modal- und jquery-Methode arbeiten, also eliminierte ich die Datentabellen jquery vom unteren Rand der index.blade.php. Mein erster Fehler war, die Daten nicht zuerst aufzurufen. Ich habe eine separate Datei namens edit.blade.php im Ordner view-admin-empresa erstellt. Hier ist der Code:

@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

Dann habe ich im web.php die folgenden Routen erstellt:

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

    Route::get('registered-user', 'Admin\[email protected]');
    Route::get('registered-empresa', 'Admin\[email protected]');
    Route::get('role-edit/{id}', 'Admin\[email protected]');
    Route::put('role-update/{id}', 'Admin\[email protected]');
    Route::post('save-empresa', 'Admin\[email protected]');
    Route::get('/edit-empresa/{id}', 'Admin\[email protected]');
    Route::put('/empresa-update/{id}', 'Admin\[email protected]');
});

Wie bereits erwähnt, habe ich die Bearbeitungsmodale eliminiert und zur Datei empresa edit.blade.php umgeleitet.