Laravel provides several ways to retrieve input from an HTTP request. Two of the most commonly used methods are input() and string(). While they look similar, they have an important difference that can make your code safer and more expressive.
input() returns mixed values
The input() method returns the raw value from the request.
$name = $request->input('name');
The return type depends entirely on the submitted data:
stringarrayintboolnull
This makes input() the right choice when you don't know or don't care about the exact type, or when you're expecting something other than a string.
For example:
$ids = $request->input('ids', []);
$published = $request->input('published', false);
string() always returns a Stringable object
If you expect textual input, string() is often the better choice.
$name = $request->string('name');
Instead of returning a plain string, it returns an Illuminate\Support\Stringable instance, allowing you to immediately chain string operations.
$username = $request
->string('username')
->trim()
->lower()
->replace(' ', '-');
No need for nested Str::of() calls or temporary variables.
Better type safety
One subtle advantage is that string() guarantees you're working with a string-like value.
With input(), it's easy to accidentally call a string function on an array:
// Potentially problematic
$name = trim($request->input('name'));
If name unexpectedly contains an array, PHP will throw a type error.
Using string() makes your intent explicit:
$name = $request
->string('name')
->trim()
->toString();
When to use which
As a general guideline:
Use input() when:
- You expect arrays, booleans, integers, or mixed data.
- You simply need the raw request value.
Use string() when:
- You expect text input.
- You plan to manipulate the value.
- You want more expressive, fluent code.
- You want to make your intent clear to future readers.
My recommendation
For textual fields like names, email addresses, search queries, slugs, or titles, prefer string(). It communicates that the value is expected to be text and gives you Laravel's fluent string API for free.
Reserve input() for cases where the value may legitimately be another type, such as arrays from multi-select fields, boolean flags, or numeric values.
It's a small change, but one that makes your codebase a little more readable, a little safer, and a little more idiomatic.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.