75

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?

3
  • What is the problem ?
    – Cyrbil
    Sep 22, 2015 at 13:57
  • Non-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context Sep 22, 2015 at 13:59
  • 2
    The code above is fine. All that is needed is the line use Illuminate\Support\Facades\Request; at the top of the file. For just the post data, Request::post() could be used instead.
    – Dave F
    May 27, 2021 at 0:08

8 Answers 8

88

Try this :

use Illuminate\Support\Facades\Request;
public function add_question(Request $request)
{
    return $request->all();
}
5
  • 20
    Wrong! It will return also GET variables.
    – barbushin
    Apr 27, 2017 at 11:50
  • 26
    @barbushin, If all answers here are wrong, provide CORRECT answer here. Aug 4, 2017 at 7:00
  • @ShaunakShukla I have provided the correct answer that only shows POST data (although your solution solves the main error the OP was having) Apr 17, 2018 at 15:39
  • 1
    @Shaunak Shukla I don't understand. You said to provide the correct answer in your comment to barbushin. So I did. Apr 19, 2018 at 13:45
  • This didn't work for me, Laravel version 5.2.45 Feb 28, 2021 at 20:45
88

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
5
  • 3
    Great answer with solid information and great explanations. Thank you, you just saved me from another hour of googling. Apr 18, 2018 at 4:53
  • 1
    nice explanation with doc references. Apr 19, 2018 at 12:53
  • Return error Method post does not exist. Also i can't find post() in documentation
    – Mohammad
    Aug 22, 2019 at 9:39
  • I should probably try and get the docs updated. But here it is in the API docs: laravel.com/api/5.8/Illuminate/Http/Concerns/… . Aug 22, 2019 at 11:40
  • Method post is only available in Laravel >= 5.5, for 5.4 and below try this answer Oct 16, 2019 at 8:17
15

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

2
  • I think my solution to this issue is better. Just do $request->post() Apr 17, 2018 at 15:40
  • 1
    $request->post() is only available in Laravel >= 5.5 Oct 16, 2019 at 8:18
6

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.

4
  • 8
    Wrong! It will return also GET variables.
    – barbushin
    Apr 27, 2017 at 11:50
  • 1
    @barbushin The question was how to retrieve the POST variables. I never said it doesn't also retrieve GET variables. Kindly read before commenting, and don't be an ass.
    – Joel Hinz
    Apr 27, 2017 at 11:52
  • 1
    If it will be GET request, then return Request::all(); will return GET variables.
    – barbushin
    Apr 27, 2017 at 14:30
  • @barbushin I have provided an answer that only gives POST data. Not both GET and POST data, Apr 17, 2018 at 15:37
3

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

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')
1
  • Hello DejavuGuy, kindly check the date that this answer was provided Sep 7, 2018 at 17:36
1

You can use it

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

without

import Illuminate\Http\Request OR

use Illuminate\Support\Facades\Request OR other.

0

It should be at least this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.