laravel update status to 0 or 1 when button is toggled in a single row using ajax
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Toggling Database Status via AJAX in Laravel: A Practical Guide
As a senior developer, I often encounter scenarios where we need to perform dynamic updates on a backend (like toggling a status in a database) without forcing a full page reload. This is the essence of modern, responsive web applications. For those new to Laravel, integrating AJAX for simple CRUD operations can seem daunting, but it is a fundamental skill.
You are facing a classic challenge: making an asynchronous request that successfully updates data and provides user feedback. Let's break down your specific requirement—toggling an is_active status in MySQL using JavaScript and Laravel’s backend—and address the issues you encountered with your initial attempt.
The AJAX Dilemma: Form vs. Pure API
You asked whether to use a standard form or pure AJAX for toggling a checkbox.
The short answer is: Pure AJAX is the superior method for this specific task.
While using a standard HTML <form> that submits via POST works, it usually results in a full page reload, defeating the purpose of asynchronous interaction. When dealing with single-item updates or toggles, we should leverage JavaScript (AJAX) to handle the request directly and manage the response without refreshing the browser.
The key is ensuring your backend endpoint responds with data (like JSON success/failure) rather than redirecting back to a view, which is what you are attempting in your controller example. This approach aligns perfectly with building robust APIs, an area where frameworks like Laravel truly shine, as demonstrated by the principles outlined on https://laravelcompany.com.
Step-by-Step Implementation: The AJAX Workflow
To achieve a smooth toggle experience with confirmation feedback, we need to refine the interaction between the frontend (Blade/JS) and the backend (Controller).
1. Refining the Frontend Interaction (Blade & JavaScript)
Instead of relying solely on the checkbox state for the update, we will pass the necessary data (id and the new desired status) directly in the AJAX request. We will also use the success callback to handle the confirmation message, mimicking a delete modal effect.
Here is how we can adjust your JavaScript to be more robust:
@section('script')
<script type="text/javascript">
$(document).ajaxStop(function () {
$('.toggle_status').on('click', function (e) {
var $checkbox = $(this);
var recordId = $checkbox.closest('tr').find('input[type="hidden"]').val(); // Get the ID safely
var newStatus = $checkbox.is(':checked') ? 1 : 0;
// Disable button during request to prevent double clicks
$(this).prop('disabled', true).text('Updating...');
$.ajax({
type: 'POST',
url: '{{ route('admin.system-functions') }}',
async: true,
data: {
id: recordId,
is_active: newStatus // Send the desired state directly
},
success: function (response) {
// Handle success and display feedback (like a Bootstrap alert/modal)
if (response.success) {
alert('Status successfully updated!');
// In a real app, you would trigger your modal here using Alpine.js or a similar library
} else {
alert('Error updating status: ' + response.message);
}
$checkbox.prop('disabled', false).text($checkbox.is(':checked') ? 'Toggle' : 'Status');
},
error: function (xhr) {
alert('An error occurred during the update.');
$checkbox.prop('disabled', false).text($checkbox.is(':checked') ? 'Toggle' : 'Status');
}
});
});
});
</script>
@endsection
2. Optimizing the Backend Logic (Controller)
Your controller logic needs to be adjusted to handle the AJAX request by returning a JSON response instead of a redirect. This allows the frontend to decide what message to display.
use App\Models\SystemFunction; // Assuming you have a model
public function update(Request $request, $id)
{
// 1. Validation (Crucial for security!)
$request->validate([
'is_active' => 'required|integer',
]);
$function = SystemFunction::findOrFail($id);
// 2. Update the record
$function->update([
'is_active' => $request->is_active, // Use the value sent from AJAX
]);
// 3. Return a JSON response instead of redirecting
return response()->json([
'success' => true,
'message' => 'Status updated successfully.',
'new_status' => $function->is_active
], 200);
}
3. Route Setup
Ensure your route is set up to handle the POST request correctly:
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'staff'), function () {
// The update method now expects the ID from the URL segment
Route::post('/system-functions/{id}', 'SystemFunctionController@update')->name('system-functions.update');
});
Conclusion
By shifting from a traditional form submission mindset to an API-driven approach using AJAX, you gain full control over the user experience. The key takeaways are: send the desired state directly, use JSON responses for communication back to the client, and handle success/error states gracefully in your JavaScript. This pattern ensures that your application is fast, responsive, and adheres to modern architectural principles, making it a powerful way to build applications on Laravel.