Sanniv Khare 6 min read

Strict request input in Laravel 13.40: loose vs strict, tested

Strict request input in Laravel 13.40: loose vs strict, tested

Laravel 13.40.0 added a strict argument to the typed request accessors integer(), float(), boolean() and string(). Without it, ?page=2,024 becomes 2 and ?hd=maybe becomes false, silently. With strict: true, any value that is not a clean integer, number, boolean word or string gives you the default you passed instead. The takeaway: pass null as the default when you use strict: true on anything that matters, because a default of 0 or false looks exactly like a real value. It does not throw and it is not validation.

We ran 17 inputs through Illuminate\Http\Request from laravel/framework 13.43.0 (with symfony/http-foundation 7.4) on PHP 8.3.6, on a Linux test server, loose and strict side by side. We also read the change itself: the diff of src/Illuminate/Support/Traits/InteractsWithData.php between the v13.39.0 and v13.40.0 tags.

Why we did this: after taking a Laravel catalogue app from 10 to 13, the controllers were full of (int) $request->input(...) and $request->boolean(...) calls written for forms that only ever sent clean values. Query strings and API clients do not.

What the new argument does

The whole feature is one new parameter on five methods (str() passes it through to string()). From the 13.40.0 diff:

Method Loose (default) With strict: true
integer($key, $default = 0) (int) $value default unless filter_var(..., FILTER_VALIDATE_INT) passes; booleans rejected
float($key, $default = 0.0) (float) $value default unless FILTER_VALIDATE_FLOAT passes; booleans rejected
boolean($key, $default = false) FILTER_VALIDATE_BOOLEAN, anything unknown is false default unless the value is 1, true, on, yes, 0, false, off or no
string($key, $default = null) new Stringable($value) default unless the value is a string, int, float or Stringable

Laravel’s own request documentation describes it the same way and uses named arguments, which is the readable form: $request->integer('count', 5, strict: true). The source is in InteractsWithData.php on the 13.x branch. The same trait is used by Fluent and other data objects, so they get the argument too.

17 inputs, loose and strict

Each row below is a real call on a POST request with one form field. The strict column passes null as the default ('n/a' for string()), so you can see when strict mode rejected the value.

Loose and strict results for 14 form inputs

The rows that matter in practice:

  • '2,024' and '12abc' become 2 and 12 when loose. That is PHP’s numeric-prefix cast, and it is how a pasted year or a typo turns into a valid-looking ID. Strict returns the default.
  • '4.7' becomes 4 for integer(). Strict rejects it rather than rounding.
  • '4,5' becomes 4.0 for float(). A decimal comma from a European keyboard quietly loses its fraction. Strict rejects it.
  • ' 7 ' and '1e3' pass strict mode. filter_var trims whitespace for integers and accepts exponent notation for floats. If you need digits only, that is a validation rule, not this flag.
  • 'maybe', '2' and '' are all false for loose boolean(). With strict and a null default you can finally tell “unchecked” from “garbage”.
  • An array for string() (?name[]=a&name[]=b) gives 'Array' plus a PHP warning (“Array to string conversion”) when loose. Strict returns the default with no warning.

JSON bodies go through the same code, with one difference worth knowing:

JSON body
integer  true         1          NULL
integer  5.0          5          5
integer  3            3          3

A JSON true sent to integer() is 1 when loose. Strict mode checks for booleans explicitly (the source has a comment about FILTER_VALIDATE_INT turning true into 1) and returns the default. A JSON 5.0 still counts as an integer.

The trap: strict with a 0 or false default

Our first test used the methods’ own defaults, and strict boolean('hd', false, strict: true) on 'maybe' returned false, the same as loose mode. Nothing changed, because the rejected value fell back to a default that is also a valid answer. The same goes for integer('page', 0, strict: true).

Loose casts next to strict calls with explicit defaults

Pick the default by what the code does next:

// Listing filters: a bad value should fall back quietly.
$page    = $request->integer('page', 1, strict: true);
$perPage = $request->integer('per_page', 20, strict: true);

// Something you act on: a bad value should be visible.
$hd = $request->boolean('hd', null, strict: true);
if ($hd === null && $request->filled('hd')) {
    abort(422, 'hd must be yes or no');
}

Strict accessors are not validation

strict: true and the validator do not agree on what a boolean is. From validateBoolean() in the same framework version, the boolean rule accepts true, false, 0, 1, '0' and '1', and boolean:strict only true and false. Strict boolean() also accepts 'yes', 'on', 'true' and their opposites. So a request can fail 'hd' => 'boolean' validation and still read as true through $request->boolean('hd', strict: true).

Our rule of thumb:

  • Writes (forms, API create and update): validate with a FormRequest and read from $request->validated(). That is where a 422 with a message belongs.
  • Reads (pagination, sorting, filters, feature flags in query strings): strict: true with a sensible default is enough, and much less code than a FormRequest per listing page.

Finding the loose calls in your app

These two searches list every place a controller casts input itself. Run them from the project root:

grep -rnE "\((int|float|bool|string)\) ?\\\$request->(input|get|query)\(" app/ routes/
grep -rnE "\\\$request->(integer|float|boolean|string|str)\(" app/ routes/

For each hit, decide which of the two groups above it belongs to, then either move it into validation or add strict: true with an explicit default. If you test controllers, add one case per accessor with a value like '12abc' or 'maybe': those are the inputs that changed behaviour in our table.

Version notes

  • The argument exists from laravel/framework 13.40.0. On 13.39 and earlier, passing a third argument is simply ignored by PHP (extra positional arguments to a user function are allowed), but a named strict: argument fails with an “Unknown named parameter” error. Bump laravel/framework before you deploy code that uses it.
  • We tested the 13.x branch at version 13.43.0 (20 Nov 2026); the four accessors have not changed since 13.40.0.
  • Laravel 13 needs PHP 8.3 or newer.

Made by the team behind this post

Apps from our studio

  • Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get
  • OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.