为什么 Laravel REST 控制器 $request->input 为 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38337528/
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-09-14 14:08:17  来源:igfitidea点击:

Why Laravel REST controller $request->input is NULL?

phprestlaravellaravel-5.2

提问by TomR

I am following tutorial http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorialand I have managed to write the similar method for my controller:

我正在关注教程http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial并且我设法为我的控制器编写了类似的方法:

public function update(Request $request, $id) {
    $employee = Employee::find($id);

    $employee->name = $request->input('name');
    //...
    $employee->save();

    return "Sucess updating user #" . $employee->id;
}

It is thought in tutorial that this code works but in reality var_dump($request->input) gives NULL. So - what $request variable should I use for getting the body of the request? I made var_dump($request) but the structure is unmanageably large. Actually I am suscpicous about this tutorial - do we really need to list all the fields in the standard update procedure?

在教程中认为此代码有效,但实际上 var_dump($request->input) 给出了 NULL。那么 - 我应该使用什么 $request 变量来获取请求的正文?我做了 var_dump($request) 但结构太大了。实际上,我对本教程持怀疑态度 -我们真的需要列出标准更新程序中的所有字段吗?

回答by swatkins

You can access the input data with:

您可以通过以下方式访问输入数据:

$input = $request->all();

https://laravel.com/docs/5.2/requests#retrieving-input

https://laravel.com/docs/5.2/requests#retrieving-input

However, I've also had to get the input in this manner when using AngularJS $httpmodule:

但是,在使用 AngularJS$http模块时,我也必须以这种方式获取输入:

$input = file_get_contents('php://input');

回答by jignesh patel

for get all input

获取所有输入

try it

尝试一下

$request = \Input::all();

回答by Akshay Khale

If you want to fetch individual parameters from request object the you can do that with input Methodof Request Class.

如果您想从请求对象中获取单个参数,您可以使用请求类的输入方法来实现。

$request->input("parameter_name");

$request->input("parameter_name");

But if you want to fetch all request parameters then you can use allmethod which will return you an array of all the request key-value pairs

但是如果你想获取所有请求参数,那么你可以使用all方法,它会返回一个包含所有请求键值对的数组

$request->all()

$request->all()

The thing you are missed is, you are calling $request->inputwhich is null because inputis methodof Request class and not a property

你错过的事情是,你在呼唤$request->input它为null,因为input方法请求类的,而不是一个属性