NULL默认值不一致,&更新将NULL更改为空字符串Laravel4

时间:2014-07-05 18:06:46

标签: php mysql laravel-4 null

我有许多字段在MySQL中默认为NULL。例如,在下面的代码片段中,name是必需的而不是null,但name_abbrev,email_general和description都可以为空,并且在数据库中默认设置为NULL。但是,通过Laravel中的表格输入数据大多不起作用。如果我插入一个只有名称且其他所有内容都留空的行,则当然输入正确的名称,并为email_general输入NULL,但其他两个可空字段会发布空字符串而不是NULL。如果我然后通过更改名称更新行,则更新还会将email_general的NULL更改为空字符串。如果我手动将可空字段的数据库行中的值从空字符串更改为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() }}

edit.blade.php:

{{ 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() }}

非常感谢!这应该很容易,但出于某种原因,它不是。

1 个答案:

答案 0 :(得分:1)

如果你真的需要在某些列上使用null,我建议你使用Mutators:

Laravel 3:http://three.laravel.com/docs/database/eloquent#getter-and-setter-methods

Laravel 4:http://laravel.com/docs/eloquent#accessors-and-mutators

因此,如果新值为空字符串(!$ value),则用NULL覆盖它。

相关问题