php 如何在 Laravel 中获取 POST 的所有输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32718870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:09:23  来源:igfitidea点击:

How to get All input of POST in Laravel

phplaravellaravel-5

提问by Abrar Jahin

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

我正在使用 Laravel 5 并尝试像这样在控制器中获取 POST 变量的所有输入 -

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

So, I am getting this errors-

所以,我收到了这个错误——

enter image description here

在此处输入图片说明

What I am doing wrong?

我做错了什么?

回答by Md Rashedul Hoque Bhuiyan

Try this :

尝试这个 :

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

回答by Yahya Uddin

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

似乎是在几乎所有的,因为它们显示当前的答案出现了重大失误BOTHGET和POST数据。并非只有POST数据。

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:

您还可以像这样使用全局请求方法(@Canaan Etai 提到),无需导入:

request()->post();

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

但是,Request在控制器方法中导入的更好方法是通过依赖注入,如@shuvrow 回答中所述:

use Illuminate\Http\Request;

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

More information about DI can be found here:

可以在此处找到有关 DI 的更多信息:

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

回答by Victor Anuebunwa

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

对于那些来到这里寻找“如何获得所有POST输入”的人

class Illuminate\Http\Requestextends from Symfony\Component\HttpFoundation\Requestwhich has two class variables that store request parameters.

Illuminate\Http\Request扩展自Symfony\Component\HttpFoundation\Request它有两个存储请求参数的类变量。

public $query- for GET parameters

public $query- 对于 GET 参数

public $request- for POST parameters

public $request- 用于 POST 参数

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.

对于 Laravel >= 5.5,您可以简单地调用$request->post()or $request->post('my_param')which 内部调用$request->request->all()or $request->request->get('my_param')

回答by Joel Hinz

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

您应该使用外观而不是Illuminate\Http\Request. 在顶部导入它:

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.

编辑:这个答案是几年前写的。我现在赞成下面 shuvrow 建议的方法。

回答by nageen nayak

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

回答by Charmie

It should be at least this:

至少应该是这样的:

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

回答by Rafael Mendes

You can use it

你可以使用它

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

without

没有

import Illuminate\Http\RequestOR

import Illuminate\Http\Request或者

use Illuminate\Support\Facades\RequestOR other.

use Illuminate\Support\Facades\Request或其他。

回答by Canaan Etai

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

使用 Dependency 比将其附加到类更好。

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

你现在可以使用laravel提供的全局请求方法

request()

for example to get the first_name of a form input.

例如获取表单输入的 first_name。

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