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() ?

miken32
  • 42,008
  • 16
  • 111
  • 154
Jan
  • 220
  • 1
  • 2
  • 8

4 Answers4

9

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

$name = Input::get('name');
Dave Amphlett
  • 1,922
  • 1
  • 15
  • 17
bogartalamid
  • 129
  • 1
  • 5
  • 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. – Dave Amphlett Apr 28 '14 at 14:25
  • 1
    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: http://laravel.com/docs/extending#request-extension – Colin Jul 25 '14 at 13:58
  • To expand on @Colin's answer, extend the Laravel Request class with your own, as explained here: http://stackoverflow.com/a/30840179/517371 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. – Tobia May 25 '16 at 15:43
  • Question: "What is it I should use instead of Input::get()" Answer: "Input::get()" – miken32 Dec 03 '21 at 17:02
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').

juliocesar
  • 5,706
  • 8
  • 44
  • 63
Ganesh Jogam
  • 3,117
  • 1
  • 13
  • 10
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');
nageen nayak
  • 1,262
  • 2
  • 18
  • 28
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.

miken32
  • 42,008
  • 16
  • 111
  • 154