Laravel 雄辩的创造,而不是插入一切
我想使用表单中的输入在数据库中插入一行 //个人控制器Laravel 雄辩的创造,而不是插入一切,laravel,eloquent,Laravel,Eloquent,我想使用表单中的输入在数据库中插入一行 //个人控制器 Public function store(){ $input = Input::all(); if(! $this->person->fill($input)->isValid()){ return Redirect::back()->withInput()->withErrors($this->person->messages); } $this
Public function store(){
$input = Input::all();
if(! $this->person->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->person->messages);
}
$this->person->create($input);
}
//班主任
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() }}
缺少外键组id,且布尔值为Responsible,这是因为表单中的名称与数据库中属性的名称不匹配
isResponsible ≠ isresponsible
group ≠ group_id
只需更改表单中的名称:
{{ Form::select('group_id', $groups, Input::old('group_id')) }}
{{-- ... --}}
{{ Form::checkbox('isresponsible') }}
{{ Form::select('group_id', $groups, Input::old('group_id')) }}
{{-- ... --}}
{{ Form::checkbox('isresponsible') }}