Jan
Jan

Reputation: 230

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

Upvotes: 11

Views: 57033

Answers (4)

miken32
miken32

Reputation: 42716

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.

Upvotes: 2

Nageen
Nageen

Reputation: 1759

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');

Upvotes: 2

bogartalamid
bogartalamid

Reputation: 129

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

$name = Input::get('name');

Upvotes: 9

Ganesh Jogam
Ganesh Jogam

Reputation: 3151

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').

Upvotes: 2

Related Questions