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
How to get All input of POST in Laravel
提问by Abrar Jahin
回答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 Request
in 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 的更多信息:
- https://laravel.com/docs/5.6/container
- https://laravel.com/docs/5.6/controllers#dependency-injection-and-controllers
- 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
回答by Victor Anuebunwa
For those who came here looking for "how to get All input of POST" only
对于那些来到这里寻找“如何获得所有POST输入”的人
class Illuminate\Http\Request
extends from Symfony\Component\HttpFoundation\Request
which 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\Request
OR
import Illuminate\Http\Request
或者
use Illuminate\Support\Facades\Request
OR 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')