变量返回为null的Laravel$请求
我正在为我的api编写更新方法。我需要更新请求的字段。 所以我试图只获取请求的字段并更新它们。然而,下面的代码返回null,即使我将$fill转换为string变量返回为null的Laravel$请求,laravel,request,Laravel,Request,我正在为我的api编写更新方法。我需要更新请求的字段。 所以我试图只获取请求的字段并更新它们。然而,下面的代码返回null,即使我将$fill转换为string foreach ($fillableFields as $fill){ $customerId->$fill = $request->get("$fill") ; 编辑-1 我的代码如下 $fillableFields = array('lastname','firstname'
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get("$fill") ;
编辑-1
我的代码如下
$fillableFields = array('lastname','firstname');
当我dd($fill)时,它返回null 您的方法应该是
public function yourMethod(Request $request)
{
try {
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get($fill) ;
}
}
} catch (\Exception $ex) {
}
}
获取请求参数的最佳方法在laravel的官方文档中提供,如下所示 您可以根据具体需要获取请求参数 通过使用下面的语句,您只能获得您在only属性中指定为数组的参数,例如:用户名、密码
$input = $request->only(['username', 'password']);
如果要获取除特定字段之外的所有字段,则可以使用except参数定义如下。通过使用下面的命令,可以获得除信用卡以外的所有字段
$input = $request->except('credit_card');
有关请求参数的更多信息,请参阅官方文档url:您忘记调用
save()
方法来更新对象:
foreach ($fillableFields as $fill){
$customerId->$fill = $request->get($fill);
$customerId->save();
}
你能给我们看一下
$filleblefields
,$customerId
et$request
的输出吗?刚刚更新了我的问题,以及$customerId是如何定义的?customerId=Customer::find($id);我试过了,但还是不起作用。它没有更新我的领域非常感谢你的兴趣,这是因为邮递员和拉威尔。在《邮递员》中,我使用的是表单数据,而不是x-www-form-urlencoded,因此当我切换时,它运行良好。