Tabish
Tabish

Reputation: 161

How to delete data with yajra datatable in laravel?

I am using Laravel Yajra dataTable How to solve this error?

Missing required parameters for [Route: plane.destroy] [URI: plane/{plane}/id]. 

My controller:

if ($request->ajax()) {
            $plane = Plane::with('presidents', 'years', 'plantypes')->selectRaw('distinct planes.*')->get();
            return DataTables::of($plane)
                ->addColumn('p_name', function (Plane $plane) {  // change this code based on your need
                    return $plane->presidents->map(function ($president) {
                        return str_limit($president->P_name);
                    })->implode('<br>');
                })         
                ->addColumn('action', function ($plane) {
                    $btn = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $plane->id . '" data-original-title="Edit" class="edit btn btn-primary btn-sm editItem">Edit</a>';
                    $btn = $btn . ' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="' . $plane->id . '" data-original-title="Delete" class="btn btn-danger btn-sm deleteItem">Delete</a>';
                    return $btn;
                })

                    ->rawColumns(['p_name', 'action'])
                    ->make(true);
                }
            return $dataTable->render('planes.index');

js code:

   $('body').on('click', '.deleteItem', function () {
            var id = $(this).data("id");
            confirm("Are You sure want to delete !");
            $.ajax({
                type: "DELETE",
                url: "{{ route('plane.destroy') }}"+'/'+id,
                success: function (data) {
                    table.draw();
                },
                error: function (data) {
                    console.log('Error:', data);
                }
            });
        });

My Route:

Route::resource('plane', 'PlaneController');

Upvotes: 0

Views: 1176

Answers (1)

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

pass url instead of route. the model you call in resource will be plane/{id}

url: "{{ url('plane') }}/"+id,


GET           /plane                      index   plane.index
GET           /plane/create               create  plane.create
POST          /plane                      store   plane.store
GET           /plane/{plane}               show    plane.show
GET           /plane/{plane}/edit          edit    plane.edit
PUT|PATCH     /plane/{plane}               update  plane.update
DELETE        /plane/{plane}               destroy plane.destroy

Upvotes: 1

Related Questions