How to get All input of POST in Laravel

PhpLaravelLaravel 5

Php Problem Overview


I am using Laravel 5 and trying to get all input of POST variable in controller like this-

public function add_question()
{
	return Request::all();
}

So, I am getting this errors-

enter image description here

What I am doing wrong?

Php Solutions


Solution 1 - Php

Try this :

use Illuminate\Support\Facades\Request;
public function add_question(Request $request)
{
    return $request->all();
}

Solution 2 - Php

There seems to be a major mistake in almost all the current answers in that they show BOTH GET and POST data. Not ONLY POST data.

The issue with your code as the accepted answer mentioned is that you did not import the facade. This can imported by adding the following at the top:

use Request;

public function add_question(Request $request)
{
    return Request::post();
}

You can also use the global request method like so (mentioned by @Canaan Etai), with no import required:

request()->post();

However, a better approach to importing Request in a controller method is by dependency injection as mentioned in @shuvrow answer:

use Illuminate\Http\Request;

public function add_question(Request $request)
{
    return $request->post();
}

More information about DI can be found here:

In either case, you should use:

// Show only POST data
$request->post(); // DI
request()->post(); // global method
Request::post(); // facade

// Show only GET data
$request->query(); // DI
request()->query(); // global method
Request::query(); // facade

// Show all data (i.e. both GET and POST data)
$request->all(); // DI
request()->all(); // global method
Request::all(); // facade

Solution 3 - Php

For those who came here looking for "how to get All input of POST" only

TLDR;

Laravel >= 5.5

$request->post() 
// or 
$request->post('my_param')

Older Versions

$request->request->all(); //Get all post requests
$request->request->get('my_param'); //Get a post parameter

Explanation

class Illuminate\Http\Request extends from Symfony\Component\HttpFoundation\Request which has two class variables that store request parameters.

public $query - for GET parameters

public $request - for POST parameters

$request->request->all(); //Get all post requests
$request->request->get('my_param'); //Get a post parameter

$request->post() for versions 5.5 and above internally calls $request->request->all() or $request->request->get('my_param') respectively.

Source [here][1]

[1]: https://stackoverflow.com/a/27367330/2836233 "In Laravel, how can I get only POST parameters?"

Solution 4 - Php

You should use the facade rather than Illuminate\Http\Request. Import it at the top:

use Request;

And make sure it doesn't conflict with the other class.

Edit: This answer was written a few years ago. I now favour the approach suggested by shuvrow below.

Solution 5 - Php

You can get all post data into this function :-

$postData = $request->post();

and if you want specific filed then use it :-

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

Solution 6 - Php

its better to use the Dependency than to attache it to the class.

public function add_question(Request $request)
{
    return Request::all();
}

or if you prefer using input variable use

public function add_question(Request $input)
{
    return $input::all();
}

you can now use the global request method provided by laravel

request()

for example to get the first_name of a form input.

request()->first_name
// or
request('first_name')

Solution 7 - Php

You can use it

$params = request()->all();

without

import Illuminate\Http\Request OR

use Illuminate\Support\Facades\Request OR other.

Solution 8 - Php

It should be at least this:

public function login(Request $loginCredentials){
     $data = $loginCredentials->all();
     return $data['username'];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbrar JahinView Question on Stackoverflow
Solution 1 - PhpMd Rashedul Hoque BhuiyanView Answer on Stackoverflow
Solution 2 - PhpYahya UddinView Answer on Stackoverflow
Solution 3 - PhpVictor AnuebunwaView Answer on Stackoverflow
Solution 4 - PhpJoel HinzView Answer on Stackoverflow
Solution 5 - Phpnageen nayakView Answer on Stackoverflow
Solution 6 - PhpCanaan EtaiView Answer on Stackoverflow
Solution 7 - PhpRafael MendesView Answer on Stackoverflow
Solution 8 - PhpCharmieView Answer on Stackoverflow