my GET method is not supported for this route. (for delete) Laravel 5.8

时间:2019-04-08 12:51:23

标签: laravel

I have table view with datatables yajra . before i using datatable my delete is normaly ,but after using datatables my delete have error like this

The GET method is not supported for this route. Supported methods: DELETE.

i using route delete but its didint work . can you correct my code ?

view

 <div class="box-body table-responsive no-padding">
            <table class="table table-hover" id="table">
              <tbody><tr>
                <thead>
                {{--  <th>No</th>  --}}
                <th>Nama Alat</th>
                <th>Waktu</th>
                <th>User Input</th>
                <th>Action</th>
                <th>Edit</th>
                <th>Hapus</th>
                <th>Tanggal</th>
                </thead>
               </tr>


@push('scripts')
    <script>
        $(function () {
            $('#table').DataTable({
                processing: true,
                serverSide: true,
                responsive: true,
                ajax: '{!! route('adminshow1dt') !!}',
                columns: [

                    {data: 'alat.nama_alat', name: 'alat.nama_alat'},
                    {data: 'status', name: 'pemeliharaan.status'},
                    {data: 'user.name', name: 'user.name'},
                    {data: 'action', name: 'action', orderable: false, searchable: false},
                    {data: 'edit', name: 'edit', orderable: false, searchable: false},
                    {data: 'hapus', name: 'hapus', orderable: false, searchable: false},
                    {data: 'created_at', name: 'created_at'},

                ],
            });
        })
    </script>
@endpush

my controller and route

public function show()
{
    // $pemeliharaan = Pemeliharaan::all();
    // $pemeliharaan = Pemeliharaan::find($id);
    $pemeliharaan = Pemeliharaan::with(['user', 'alat'])->where('status', 'harian')->get();

    return view('admin.view_harian', ['pemeliharaan' => $pemeliharaan]);
}

public function indexDataTablesh()
{
    $pemeliharaan = Pemeliharaan::with(['user', 'alat'])->where('status', 'harian')->get();

    return Datatables::of($pemeliharaan)
        ->addColumn('action', function ($pemeliharaan) {
            return '<a href="/admin/show/question/' . $pemeliharaan->id . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-eye-open"></i> View Data</a>';
        })
        ->editColumn('edit', function ($pemeliharaan) {
            return '<a href="/admin/' . $pemeliharaan->id . '/edit_harian" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
        })
        ->editColumn('hapus', function ($pemeliharaan) {
            return '<a href="/admin/delete1/' . $pemeliharaan->id . '" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-remove-circle"></i> Hapus</a>';
        })
        ->rawColumns(['hapus' => 'hapus', 'action' => 'action', 'edit' => 'edit'])
        ->make(true);
}

Routes

Route::delete('/admin/delete1/{id}', 'adminController@destroy1' )->name('delete1');
Route::get('admin/show1', 'adminController@show')->name('adminshow1');
Route::get('admin/show1-dt', 'adminController@indexDataTablesh')->name('adminshow1dt');

can you correct this code plz ?

2 个答案:

答案 0 :(得分:0)

yaa, ok I got it actually a tag not support delete method if you want to use delete method then you have to use form else use get method in a tag.

Route::get('/admin/delete1/{id}', 'adminController@destroy1' )->name('delete1');

Hope this helps :)

答案 1 :(得分:0)

Try add a form to your action column, notice we have method_field "delete" and csrf_field

 $c = csrf_field();
                $m = method_field('DELETE');

         return "<form action='admin/delete1/$pemeliharaan->id' method='POST'>
                      $c
                    $m

                    <button style='margin-left:10px; width: 150px;' type='submit'
                            class='btn btn-xs btn-danger'>
                        <i class='glyphicon glyphicon-remove-circle'></i> Hapus
                    </button>
                </form>"