Reputation: 265
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
Upvotes: 0
Views: 449
Reputation: 4248
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!
Upvotes: 1