How to get All input of POST in Laravel

后端 未结 8 886
不知归路
不知归路 2021-02-03 17:10

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


        
相关标签:
8条回答
  • 2021-02-03 17:36

    It should be at least this:

    public function login(Request $loginCredentials){
         $data = $loginCredentials->all();
         return $data['username'];
    }
    
    0 讨论(0)
  • 2021-02-03 17:38

    You can use it

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

    without

    import Illuminate\Http\Request OR

    use Illuminate\Support\Facades\Request OR other.

    0 讨论(0)
  • 2021-02-03 17:44

    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:

    • https://laravel.com/docs/5.6/container
    • https://laravel.com/docs/5.6/controllers#dependency-injection-and-controllers

    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
    
    0 讨论(0)
  • 2021-02-03 17:46

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

    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

    Usage: To get a post data only

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

    Source here

    EDIT

    For Laravel >= 5.5, you can simply call $request->post() or $request->post('my_param') which internally calls $request->request->all() or $request->request->get('my_param') respectively.

    0 讨论(0)
  • 2021-02-03 17:48

    Try this :

    use Illuminate\Support\Facades\Request;
    public function add_question(Request $request)
    {
        return $request->all();
    }
    
    0 讨论(0)
  • 2021-02-03 17:54

    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')
    
    0 讨论(0)
提交回复
热议问题