好了,我已经没有主意了,我的研究已经到了终点。
有一个带有出生日期选择字段的注册表单,示例如下:
<div class="row">
<div class="col-xs-2">
{!! Form::selectMonth('dob_month', 1, ['class'=> 'form-control']) !!}
</div>
<div class="col-xs-2">
{!! Form::selectRange('dob_date', 1, 31, 1, ['class'=> 'form-control']) !!}
</div>
<div class="col-xs-2">
{!! Form::selectYear('dob_year', 1930, 1997, 1991, ['class'=> 'form-control']) !!}
</div>
</div>
当然是在laravel的刀片模板里。
你可以看到它的3个独立的选择表单dob_month, dob_date和dob_year以及现在在用户模型中的mutator中的值
private function setDobAttribute($dob){
$dobString = ['dob_year', 'dob_month', 'dob_date'];
$this->attributes['dob'] = implode('-', array_values($dobString));
}
使用dob
作为sql列名。
The Issue
我想捕获dob_year
, dob_month
, dob_date
,这将被结构化为1997-12-01,使用implode('-'array_values($dobString))
,然后保存到数据库。
当我在注册一个新用户的过程中,我得到了Undefined index: dob的错误消息。
现在我也得到的感觉,在突变函数setDobAttribute($dob)
我错过了一些东西在该函数代码,但似乎不能把我的手指放在它是什么。
输入?建议吗?
提前感谢。
使用array_map
可能是…
private function setDobAttribute($dob){
$keys = ['dob_year', 'dob_month', 'dob_date'];
$this->attributes['dob'] =
implode('-', array_map(function($key) use($dob){
return $dob[$key];
}, $keys));
return $this;
}
private function setDobAttribute(){
$array = array('dob_year', 'dob_month', 'dob_date');
return $this->attributes['dob'] = implode(-', $array);
}