Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving GET and POST data inside Laravel controller

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

like image 556
Jan Avatar asked Sep 12 '13 10:09

Jan


1 Answers

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.

like image 138
miken32 Avatar answered Oct 29 '22 23:10

miken32