0

I am using following code in javascript function

 $.ajax({
    type: "DELETE",
    url: '/delprofile',
    success:alert("Record deleted."),
        error:  alert("Record not deleted.")
});

and my route and function are as :

public function delprofile (Request $request){  
   DB::table('education')->where('id','=',7)->delete();
   return true;
}

Route::post('/delprofile','ProfileController@delprofile');

The query is not performing any deletion.

3
  • 1
    It should be Route::delete I think Commented Aug 31, 2017 at 14:29
  • I would temporarily switch the route to GET and call the URL directly. Or debug via curl. Commented Sep 1, 2017 at 11:42
  • @BjörnTantau yes it execute delete query then Commented Sep 1, 2017 at 11:45

3 Answers 3

4

As your AJAX is setting the request-method to DELETE you'll have to do the same with your route.

Route::delete('/delprofile','ProfileController@delprofile');
Sign up to request clarification or add additional context in comments.

8 Comments

still not deleting
Is the correct controller method called? Are you getting any errors?
no error, I have given all code, the main issue controller method is not being called
@YusufEkaSayogana code is being executed now but ajax error is Whoops, looks like something went wrong. while delprofile function contains only line: DB::table('education')->where('id','=',7)->delete();
@MuhammadMuazzam turn on the debug mode dude, so you can see the error, not only "Whoops"
|
1

I think the main issue comes from your JS code, you're missind the anonymos function in the both success & error :

$.ajax({
    type: "DELETE",
    url: '/delprofile',
    success: function(){ alert("Record deleted.") },
    error:  function(){ alert("Record not deleted.") },
});

Or you could also use done/fail instead :

$.ajax({
    type: "DELETE",
    url: '/delprofile',
}).done(function() {
    alert("Record deleted.");
}).fail(function() {
    alert("Record not deleted.");
});

Hope this helps.

2 Comments

I have tried both but they are not deleting any record. Even alert(10) after this code is not being executed.
Ok please check the network/console tabs in your browser developer tools, to make sure there's no error and to make sur if the request was sent or not, if the alert after you ajax query not executed that mean there's a JS error in you code...
0

added CSRF token in ajax code

data: {'_token': $('meta[name="csrf-token"]').attr('content')
            },

Discussed here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.