logo
podcast Podcast
get help Get Unstuck

Stop using $request->get()

There is a better way

Joel Clermont
Joel Clermont
2024-01-12

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.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.