Retrieving GET and POST data inside Laravel controller
Asked Answered
S

4

10

I've been searching the web for how to get POST data inside the controller, so far I have found two solutions: Input::get() and $_POST.

The comment for Input::get() reads:

/**
 * Gets a "parameter" value.
 *
 * This method is mainly useful for libraries that want to provide some flexibility.
 *
 * Order of precedence: GET, PATH, POST
 *
 * Avoid using this method in controllers:
 *
 *  * slow
 *  * prefer to get from a "named" source
 *
 * It is better to explicitly get request parameters from the appropriate
 * public property instead (query, attributes, request).
 *
 * @param string  $key     the key
 * @param mixed   $default the default value
 * @param Boolean $deep    is parameter deep in multidimensional array
 *
 * @return mixed
 */

What is this "named" source they refer to? What is it I should use instead of Input::get() ?

Stibine answered 12/9, 2013 at 10:18 Comment(6)
They probably refer to $_GET or $_POST, i.e. if you know where your parameter comes from there is not need to test first GET and then POST parameters.Inceptive
Jan, where did you get the comments from?Presage
The comment comes from the Symfony component used by Laravel 4: Symfony/Component/HttpFoundation/Request.phpStibine
See "#13240665" for your answer.Presage
@RobGordijn How do we get the $request object?Macfadyn
with App::request()Presage
C
9

The documentation shows you can retrieve an input value for any HTTP verb by using Input::get().

$name = Input::get('name');
Cull answered 30/1, 2014 at 5:52 Comment(4)
To be a little more explicit - the documentation says: "You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs." and that's the Input::get() method. If you need to differentiate between verbs, you can use different routes for different verbs, or use Request::isMethod('post') for example to determine if you're handling a HTTP POST request.Craftwork
I'm personally not a fan of this behaviour and prefer getting only input from the request body on POST requests and, similarly, only input from the query string on GET requests. I do really like using Input::get however so I've extended the Laravel Request class and overrode the input method so that it doesn't add the query string parameters to every search set. It was a very simple change. Extending the Request class is well documented here: laravel.com/docs/extending#request-extensionPrevenient
To expand on @Colin's answer, extend the Laravel Request class with your own, as explained here: https://mcmap.net/q/689099/-extend-request-class-in-laravel-5 Then, in your class, copy the input() method from Illuminate\Http\Request and remove ` + $this->query->all(). Bingo! Now in a POST request, Request::query()` returns the query (URL) parameters, while Request::input() returns the form / multipart / JSON / whatever parameters.Alainaalaine
Question: "What is it I should use instead of Input::get()" Answer: "Input::get()" 🤔Gorgerin
E
2

TO get all the inputs use Input::all() method. To check if specific column exists use Input::has('column_name') eg.Input::has('name'). To retrieve column value use Input::get('column_name') eg. Input::get('name').

Eileen answered 28/4, 2014 at 7:15 Comment(0)
D
2

You can get a parameter from url using :-

request()->urlParam;

if you want to get GET parameter using :-

$request->get('current-password');

if you want to get POST parameter using :-

$request->post('current-password');
Danedanegeld answered 16/7, 2018 at 6:22 Comment(0)
G
2

In modern Laravel installs, if your controller method is passed an instance of Request then you can use that. For example, all these are identical:

public function update(Request $request, Model $model) {
    $some_var = $_POST["some_var"];
    $some_var = $request->input("some_var");
    $some_var = $request->post("some_var");
    $some_var = $request->some_var;
}

If your method is not passed an instance of the current Request you can use the request() helper method to access one.

Gorgerin answered 18/3, 2021 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.