Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Stop using $request->get()

There is a better way

Joel Clermont
Joel Clermont
2024-01-12

From our daily Laravel tips email. Get the next one in your inbox.

We often have more than one way to do something in Laravel. Sometimes, it's just a matter of preference, but other times there are fundamental differences between the approaches.

For example, $request->get() and $request->input() might seem interchangeable, but they behave differently in some cases.

The big difference that I've bumped into is that input allows you to use "dot notation" to access nested values in arrays, but get does not.

While Laravel hasn't removed the get method from the Request class, it does include a helpful comment pointing you in the right direction:

/**
 * This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
 *
 * Instead, you may use the "input" method.
 *
 * @param  string  $key
 * @param  mixed  $default
 * @return mixed
 */
public function get(string $key, mixed $default = null): mixed
{
    return parent::get($key, $default);
}

Even better, I'd recommend using $request->validated() or $request->safe() instead, but that's a topic for another day.

Here to help,

Joel

P.S. If you want to know why we feel so strongly about validation, check out our book on the topic.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.