lundi 15 juillet 2019

I want to delete an entry using ajax in laravel 5.8

I want to delete an entry using ajax in laravel 5.8 but every time i get an errors like csrf token mismatch or post method not allowed or delete method not allowed.

I got a solution for all above problems.but now i am getting errors in deleting data.I am using model for deleting data not controller.I am using $(model_name)->(model_function) to use model in controller.I have successfully added data using this method but now i am getting error.I have attached my code and screenshot of error below.I have used another model in my current model.Table used in database is user table.

View.blade.php `html

<meta name="csrf-token" content="">
<button class='delete_user' data-id="">Delete</button>

`

ajax.js

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$(document).on('click','.delete_user',function(){
    var id = $(this).attr('data-id');
    var token = $("meta[name='csrf-token']").attr("content");
    $.ajax({
        type: 'DELETE',
        url: "ajax/"+ id,
        data: {
            '_token': token,
            'id': id
        },
        success: function (response) {
            console.log("ASD");
        }
    });
});

ajaxcontroller.php

public function destroy(ajax $ajax,$id)
    {
        $data = $ajax->delete_user($id);
        echo json_encode($data);
   }

ajax.php

public function delete_user($id)
    {
        if(user::find($id)->delete()){
            echo true;
        }else{
            echo false;
        }
    }

`

Route::resource('ajax', 'ajaxController');

I expected 1,but i got

an error SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ajax.ajaxes' doesn't exist (SQL: select * from 'ajaxes' where id = 52 limit 1)"

Screenshot of Error



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire