对不起,如果这个问题是其他地方问的,但我到处搜索,但无法找到答案。好吧,我面临这个问题上Laravel 5.0,当我尝试从一个请求得到一个变量的,即时得到空值生产服务器上但IM的开发服务器上得到一个空字符串。该值在请求中不存在,即当表单提交时该字段为空。例如。laravel检索请求输入返回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表上有'默认0'
为什么会发生这种情况?
UPDATE:
这段时间我认为请求 - >输入()方法返回空字符串(“”),如果字段不存在。但只知道我看着laravel源看到这个:
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all() + $this->query->all();
return array_get($input, $key, $default);
}
这里它返回null作为默认值。那么为什么我在开发服务器上获得空字符串?
我试过$请求 - >只(),但它也返回null。这是由不同的PHP版本呢? – bazi
我在说你应该*不*请求 - >只有 –
好。但如果我使用一个数组像$ data = ['name'=> $ request-> name,'price'=> $ request-> price]; ,$ data ['price']仍然为空。这也不能解决我的问题。 – bazi