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();
}
<
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