【发布时间】:2016-07-07 07:54:26
【问题描述】:
对不起,如果这个问题在其他地方被问到,但我到处搜索但找不到答案。好吧,我在 Laravel 5.0 上遇到了这个问题,当我尝试从请求中获取变量时,我在 Production Server 上获得了 null 值,但我得到了一个 开发服务器上的>空字符串。请求中不存在该值,即提交表单时该字段为空。例如。
// ProductController
public function store(Request $request) {
// this field is actually empty in the submitted form
$price = $request->price;
// when it gets to the server this is how this value looks like :
// $price in production server : null
// $price in development server : ''
}
当我尝试将对象保存到数据库中时
Product::save($request->only('name', 'price'));
我收到此错误(仅在生产服务器上)
SQLSTATE[23000]:违反完整性约束:1048 列“价格” 不能为空
注意价格列在mysql表上有'default 0'
为什么会这样?
更新:
一直以来,我认为如果该字段不存在,request->input() 方法会返回空字符串('')。但只知道我查看了 laravel 源代码:
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all() + $this->query->all();
return array_get($input, $key, $default);
}
这里它返回 null 作为默认值。那为什么我在开发服务器上得到空字符串?
【问题讨论】:
标签: php mysql string laravel null