Php 空默认值不一致工作,&;更新将NULL更改为空字符串Laravel4
我在MySQL中有许多默认为空的字段。例如,在下面的代码片段中,name是必需的且不为null,但是name_abbrev、email_general和description都可以为null,并且在数据库中默认设置为null。但在Laravel中,通过表格输入数据的方式大多不起作用。如果我插入一行,其中只有name,其他所有内容留空,那么name当然输入正确,email_general输入NULL,但是其他两个可为NULL的字段将显示空字符串,而不是NULL。如果我随后更新行,比如更改名称,那么更新也会将email_general的NULL更改为空字符串。如果我手动将数据库行中可空字段的值从空字符串更改为NULL,那么当我更新行时(仍然将所有空字段保留为空),它会将所有空字段更改为空字符串。我找不到我做错了什么。为什么不输入NULL(除了一个字段的编码与其他字段完全相同),为什么update甚至会将该字段更改为空字符串 控制器:Php 空默认值不一致工作,&;更新将NULL更改为空字符串Laravel4,php,mysql,laravel-4,null,Php,Mysql,Laravel 4,Null,我在MySQL中有许多默认为空的字段。例如,在下面的代码片段中,name是必需的且不为null,但是name_abbrev、email_general和description都可以为null,并且在数据库中默认设置为null。但在Laravel中,通过表格输入数据的方式大多不起作用。如果我插入一行,其中只有name,其他所有内容留空,那么name当然输入正确,email_general输入NULL,但是其他两个可为NULL的字段将显示空字符串,而不是NULL。如果我随后更新行,比如更改名称,那么
public function store()
{
$component = new Component;
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
public function update($id)
{
$component = $this->component->find($id);
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
create.blade.php:
{{ Form::open(['route' => 'components.store']) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email','General Email:') }}
{{ Form::text('email',NULL) }}
</div>
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Add New Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
{{ Form::model($component, array('method'=>'put','route'=>array('components.update', $component->id))) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email_general','General Email:') }}
{{ Form::text('email_general', NULL) }}
</div
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Update Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
{{Form::open(['route'=>'components.store'])}
{{Form::label('name','name:')}
{{Form::text('name')}
{{$errors->first('name')}
{{Form::label('name_abbrev','缩写:')}
{{Form::text('name_abbrev',NULL)}
{{Form::label('email','General email:')}
{{Form::text('email',NULL)}
{{Form::label('description','description:')}
{Form::textarea('description',NULL,['size'=>'26x3'])}
...
{{Form::submit('addnewcomponent',array('class'=>'button'))}
{{Form::close()}}
edit.blade.php:
{{ Form::open(['route' => 'components.store']) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email','General Email:') }}
{{ Form::text('email',NULL) }}
</div>
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Add New Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
{{ Form::model($component, array('method'=>'put','route'=>array('components.update', $component->id))) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email_general','General Email:') }}
{{ Form::text('email_general', NULL) }}
</div
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Update Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
{{Form::model($component,array('method'=>'put','route'=>array('components.update',$component->id))}
{{Form::label('name','name:')}
{{Form::text('name')}
{{$errors->first('name')}
{{Form::label('name_abbrev','缩写:')}
{{Form::text('name_abbrev',NULL)}
{{Form::label('email_general','general email:')}
{{Form::text('email_general',NULL)}
'26x3'])}
...
{{Form::submit('updatecomponent',array('class'=>'button'))}
{{Form::close()}}
非常感谢!这应该很容易,但由于某些原因,它不是。如果您确实需要在某些列上使用null,我建议您使用Mutators: 拉威尔3: 拉维4: 因此,如果新值是空字符串(!$value),请将其覆盖为NULL。谢谢——我现在已经学会了如何使用mutators,它完美地解决了我的问题!