How to get checkbox value in a Laravel controller?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Get Checkbox Values in a Laravel Controller: Avoiding the NULL Trap
Dealing with form data, especially boolean-like inputs such as checkboxes, often presents subtle pitfalls. You are trying to achieve a simple 1 or 0 value based on a checkbox state, but instead of getting the expected value, you are encountering NULL in your Laravel controller. This is a very common issue rooted in how HTML forms handle unchecked elements during submission.
As a senior developer, I can tell you that this problem usually stems from the interaction between client-side form behavior and server-side data reception. We can absolutely solve this without relying on client-side JavaScript like jQuery, by focusing on correct HTML structure and robust PHP handling within your Laravel application.
Understanding the Checkbox Submission Behavior
The reason you are seeing NULL is because a checkbox only sends data to the server if it is explicitly checked. If an input field is not present in the HTTP request payload, or if its value is empty when submitted, Laravel's automatic binding mechanisms might interpret this absence as NULL.
When a checkbox is unchecked, it simply does not send a value for that field during the POST request. Your controller receives what was sent, and if nothing was explicitly sent for category_is_menu, it defaults to NULL or an empty string, depending on how you access it.
The Blade/View Fix: Ensuring Data is Always Sent
The key to solving this lies in ensuring that every checkbox always submits a value, even when unchecked. This forces the server to receive a defined state (either 'on' or 'off').
In your provided example, you are attempting to use {{ old('category_is_menu', isset($category) ? 'checked' : '') }}. While this is clever for redisplaying old data, it doesn't guarantee the submitted value is present if the user simply leaves it unchecked.
For binary (true/false) values like checkboxes, the standard best practice is to ensure that the value attribute reflects the desired numeric state (1 or 0) when checked, and nothing at all (or an empty string) when unchecked.
Here is how you should structure your input for reliable data transfer:
<form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
@csrf
{{-- Set the value to '1' only if checked, otherwise it defaults to an empty string --}}
<div class="form-group">
<label>
<input type="checkbox" name="category_is_menu" value="1"
{{ old('category_is_menu', '') == '1' ? 'checked' : '' }}/>
</label>
</div>
<input class="btn btn-success" type="submit" value="Submit">
</form>
By setting a fixed value (e.g., value="1") and conditionally adding the checked attribute based on the existing data or form state, you ensure that something is sent for this field during submission.
The Controller Fix: Safe Handling of Input
Once you ensure the frontend sends a value (even if it's an empty string), the controller needs to be robust enough to handle these inputs safely without throwing errors or returning NULL unexpectedly. We use PHP’s null-coalescing operator (??) for this purpose, which is highly recommended in modern Laravel development.
use Illuminate\Http\Request;
public function store(Request $request)
{
// Use the ?? operator to default the value to 0 if the request data is missing or empty.
$isMenu = $request->input('category_is_menu', 0);
// If you expect a strict boolean (true/false), you can cast it:
// $isMenu = (bool) $request->input('category_is_menu', false);
$category = new Category();
// Store the safely retrieved integer value
$category->category_is_menu = (int) $isMenu;
return $category;
}
By using $request->input('category_is_menu', 0), if the checkbox is unchecked and nothing is sent, Laravel receives an empty string or nothing. The input() method handles this gracefully, and by explicitly providing a default value of 0, you guarantee that $isMenu will always be an integer (either the submitted value or 0), effectively solving your NULL issue. This approach aligns perfectly with the principles of clean data handling often emphasized in frameworks like Laravel.
Conclusion
The mystery of the missing checkbox value is usually a symptom of mismatched expectations between the client and server. By fixing the HTML structure to ensure all fields submit data, and by implementing defensive coding techniques like PHP's null-coalescing operators in your controller, you can reliably retrieve your boolean state (1 or 0) from the request. Remember, focusing on correct input handling is a core skill when building robust applications with Laravel. For more deep dives into Eloquent relationships and data persistence patterns, check out the official documentation at https://laravelcompany.com.