0

i'm creating function delete in laravel and here is my Controller

public function removePetitions($id)
{
    $rm = Petitions::find($id);
    $rm->delete();
    return ['status' => true];
}

and here is my route

Route::post('/delete/petition/{id}','Admin\PetitionController@removePetitions')->name('admin.delete');

when i click button delete in view it show MethodNotAllowedHttpException. anyone solve that??? thank u

15
  • In way you are using id I think you have get call so try with Route::get... Commented Dec 16, 2017 at 4:30
  • i using ajax to submit action, and method in ajax i'm using POST Commented Dec 16, 2017 at 4:36
  • In case you are using ajax, did you send CSRF token in call? Commented Dec 16, 2017 at 4:42
  • in view i used <a href="{{ route('admin.delete',$petition->id)}}" class="pull-right btn-del-petition" data-id="{{$petition->id}}">Del</a> and it's response right url i defined in route but it's still not working Commented Dec 16, 2017 at 4:42
  • You are deleting record using ajax call ? @NguyễnMinhHuy Commented Dec 16, 2017 at 4:42

1 Answer 1

1

If i understand your problem You are looking for this kin of stuff

Your anchor tag here look like this:-
<a href="javascript:;" class="pull-right btn-del-petition" data-id="{{$petition->id}}">Del</a>
And route looklike this :-
Route::post('/delete/petition','Admin\PetitionController@removePetitions');

And now ajax code :-

<meta name="csrf-token" content="{{ csrf_token() }}" /> // add in head tag
$.ajaxSetup({
headers:{
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}); 
$(document).on('click','.btn-del-petition',function(){
var id = $(this).attr('data-id');
$.ajax({
    type: 'post',
    url: 'delete/petition',
    data: {id :id},
    success:function(resp){
        alert(resp);
        //Delete that deleted row with jquery
    },
    error:function(){
        alert('Error');
    }
})
})

Now your function :-

public function removePetitions(Request $request)
{
if($request->ajax()){
    $data = $request->all();
    $rm = Petitions::find($data['id']);
    $rm->delete();
    return ['status' => true];
}
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

To work this, won't it needed to change the route from '/delete/petition/{id}' to '/delete/petition'
see my route now

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.