Laravel: Check if data is integer on controller
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel: How to Robustly Check if Input Data is an Integer in a Controller
As developers working with frameworks like Laravel, we constantly deal with incoming data from HTTP requests. A common task is ensuring that the data received—whether from form submissions, API calls, or route parameters—is of the expected type. Specifically, when expecting an integer, simply checking if a string *looks* like a number is not enough; you must ensure it is mathematically valid and safe before proceeding with database operations.
This post will walk you through the most robust and idiomatic ways to check if an input value from a Laravel request is indeed an integer, moving beyond simple comparison to implement true data validation.
---
## The Pitfall of Simple Comparison
Let's first address the example snippet provided:
```php
if ($request->input('public') == (int) $request->input('public')) {
// ...
}
```
While casting a string to an integer (`(int) $value`) will coerce the value, this approach is inherently risky. If `$request->input('public')` contains non-numeric text (e.g., `"abc"` or `"10.5"`), PHP will attempt type juggling. If the input is `"abc"`, casting it results in `0`. This leads to false positives where invalid data is treated as a valid integer, which can cause silent failures later in your application logic or database interactions.
We need a validation mechanism that handles errors explicitly rather than relying on implicit type coercion.
## Method 1: The Laravel Way – Input Validation Rules (Recommended)
The most idiomatic and powerful way to handle input checking in Laravel is by utilizing the built-in validation system defined within your Request classes or directly in your Controller methods. This shifts the responsibility of data integrity to the framework level, making your application much cleaner and more secure.
When defining rules, Laravel provides specific constraints perfect for numeric checks:
```php
// In your Controller method or Request class
public function store(Request $request)
{
$request->validate([
'public_id' => 'required|integer', // Enforces that the input must be an integer
'age' => 'required|min:18|integer', // Example with multiple constraints
]);
// If validation fails, Laravel automatically throws a validation exception.
// If it passes, we know $request->input('public_id') is a valid integer.
$publicId = $request->input('public_id');
// Proceed with database operations...
}
```
By using the `integer` rule, Laravel automatically handles the necessary string-to-integer conversion and throws an error if the input cannot be safely cast to an integer. This is a core principle of building stable applications on the **Laravel** framework.
## Method 2: Manual Runtime Type Checking (For Specific Logic)
If you need a quick, runtime check *outside* of formal validation—perhaps for specific conditional logic or logging—you must use PHP's native functions to verify if a string is purely numeric and can be safely converted. We combine `is_numeric()` with casting:
```php
public function processData(Request $request)
{
$inputValue = $request->input('public');
// Check if the input is numeric AND ensure it is an integer (no decimal part)
if (is_numeric($inputValue) && strpos($inputValue, '.') === false) {
// Safely cast to integer now that we've validated it
$integerValue = (int) $inputValue;
// Use the value safely
echo "Input is a valid integer: " . $integerValue;
} else {
// Handle the error case
throw new \InvalidArgumentException("The input 'public' must be a whole number.");
}
}
```
Notice that using `is_numeric()` combined with checking for the decimal point (`strpos($inputValue, '.') === false`) gives us much tighter control than simple casting. This manual approach is useful when you need custom error messages or specific flow control based on input quality.
## Conclusion: Prioritize Validation
For any application built on Laravel, **Method 1 (Input Validation Rules)** should always be your first line of defense. It ensures data integrity at the entry point, prevents invalid data from reaching your business logic, and adheres to the principles of clean, maintainable code that underpin good software architecture, much like the philosophy driving modern frameworks found at [https://laravelcompany.com](https://laravelcompany.com). Use manual checks sparingly, reserving them only for highly specific runtime conditions.