Laravel 5.8 请求值为空而不是默认值
Laravel 5.8 request value is null instead of default value
我用的是Laravel 5.8,如果描述为空,我想简单地使用一个默认值,然后进行总结。
// summary variable request is equal to "test"
$summary = $request->get('summary', null);
$request->get('description', $summary)
但是,该字段存在,但为空,并且描述给我 null 而不是汇总值。汇总值为 "test".
可能的解决方案
我的第一印象是数据可能没有正确发送,但再次查看您的代码后,我意识到您使用的是更不推荐使用的函数 ->get('description')
。
请尝试使用 ->input('description)
。我个人从未使用过 ->get()
,所以这可能是问题所在。
要从请求中获取信息,您应该使用 get()、input() 或直接使用名称。没有有关最近 Laravel 版本请求的 get 方法的文档。对于 Laravel 5.8 上的 input 方法,文档说
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request
它说它只有在它不存在时才有效,所以我会像这样简单地做
$description = $request->description ? $request->description : $request->summary
这实际上取决于您在所有这一切之后想要实现的目标以及您对数据的需求。
我用的是Laravel 5.8,如果描述为空,我想简单地使用一个默认值,然后进行总结。
// summary variable request is equal to "test"
$summary = $request->get('summary', null);
$request->get('description', $summary)
但是,该字段存在,但为空,并且描述给我 null 而不是汇总值。汇总值为 "test".
可能的解决方案
我的第一印象是数据可能没有正确发送,但再次查看您的代码后,我意识到您使用的是更不推荐使用的函数 ->get('description')
。
请尝试使用 ->input('description)
。我个人从未使用过 ->get()
,所以这可能是问题所在。
要从请求中获取信息,您应该使用 get()、input() 或直接使用名称。没有有关最近 Laravel 版本请求的 get 方法的文档。对于 Laravel 5.8 上的 input 方法,文档说
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request
它说它只有在它不存在时才有效,所以我会像这样简单地做
$description = $request->description ? $request->description : $request->summary
这实际上取决于您在所有这一切之后想要实现的目标以及您对数据的需求。