I have a toggle button, which will change the status (Show/Hide). I am sending proper data in ajax and try to consol and it shows correct data but in the controller, the request is empty. I have added CSRF token already and it is not the token issue. I don't know why? Here is my complete code:
$status_btn = '<div class="form-check form-switch ps-0 bg-primary ms-auto my-auto">
<input class="form-check-input ms-auto mt-1' . ($firmware->status == 1 ? ' bg-primary ' : '') . '" type="checkbox" data-id="' . $firmware->id . '" id="status_update" ' . ($firmware->status == 1 ? "checked" : "") . '>
</div>';
This is my Jquery AJAX code:
$(document).on('click', '#status_update', function(e) {
if ($(this).prop('checked')) {
$(this).addClass('bg-primary border-2');
} else {
$(this).removeClass('bg-primary');
}
var data = {
id: $(this).data('id'),
status: $(this).prop('checked') == true ? 1 : 0,
};
var url = "{{ route('changeFirmwareStatus') }}";
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "json",
processData: false,
contentType: false,
success: function(response) {
toastr.options = {
"closeButton": true,
"progressBar": true,
"showDuration": 300,
"timeOut": 2000,
"hideDuration": 1000,
// "preventDuplicates": true,
}
toastr[response.state](response.message);
}
});
});
This is my controller Route:
Route::post('/changeFirmwareStatus', [FirmwareController::class, 'changeFirmwareStatus'])->name('changeFirmwareStatus');
This is my controller code:
public function changeFirmwareStatus(Request $request)
{
dd($request->all());
$firmware = Firmware::findOrFail($request->id);
$firmware->update(['status' => $request->status]);
if ($firmware == true) {
return $this->JsonResponse(200, 'success', 'Firmware Status Successfully Changed !');
} else {
return $this->JsonResponse(422, 'error', 'Something Went Wrong !');
}
}
Take a look and guide me, (I do not want GET method and query string). Thanks in Advance
datacontains anything? did youconsole.log()?