雄辩的创造不插入一切

时间:2014-12-15 13:19:12

标签: laravel eloquent

我想使用表单中的输入在我的数据库中插入一行。

// PersonController

Public function store(){ 
    $input = Input::all();
    if(! $this->person->fill($input)->isValid()){
        return Redirect::back()->withInput()->withErrors($this->person->messages);
    }
    $this->person->create($input);
}

// class Person

protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible'];
public $messages;
public function isValid(){
    $validation = Validator::make($this->attributes, static::$rules);
    if ($validation->passes()) return true;
    $this->messages = $validation->messages();
    return false;
}

//视图中的表单

{{ Form::open(array('action' => 'PersonController@store')) }}
{{ Form::text('name') }}
{{$errors->first('name', '<span class=error>:message</span>')}}
{{ Form::select('group', $groups, Input::old('id')) }}
{{ Form::email('email') }}
{{ Form::text('phone') }}
{{ Form::text('address') }}
{{ Form::checkbox('isResponsible') }}
{{ Form::submit('Create') }}
{{ Form::close() }}

生成的sql语句
$this->person->create($input)

缺少外键(group_id)和布尔值(负责)

1 个答案:

答案 0 :(得分:0)

这是因为表单中的名称与数据库中的属性名称不匹配。

isResponsible ≠ isresponsible

group ≠ group_id

只需更改表单中的名称:

{{ Form::select('group_id', $groups, Input::old('group_id')) }}
{{-- ... --}}
{{ Form::checkbox('isresponsible') }}
相关问题