正如标题所说,我在同一页面上有2 datepickers。当我把它们都填满时,最后一个值被保存到第一个变量中,最后一个变量被设置为数据库中的0000:00:00 00:00:00。
查看
<!-- First picker -->
<input class="form-control datetimepicker" required="required" name="start_date" type="text" id="start_date">
<!-- Second picker -->
<input class="form-control datetimepicker" required="required" name="close_date" type="text" id="close_date">
<script type="text/javascript">
$(function () {
$('.datetimepicker').datetimepicker({
locale: 'nl',
sideBySide: true
});
});
</script>
控制器
public function store(Request $request)
{
$this->validate($request, [
'start_date' => 'required|date_format:d-m-Y H:i|after:tomorrow',
'close_date' => 'required|date_format:d-m-Y H:i|after:start_date',
]);
Election::create($request->all());
}
模型
protected $dates = [
'start_date',
'close_date'
];
public function setStartDateAttribute($date)
{
$this->attributes['start_date'] = Carbon::createFromFormat('d-m-Y H:i', $date, 'Europe/Amsterdam')->toDateTimeString();
}
public function setCloseDateAttribute($date)
{
$this->attributes['start_date'] = Carbon::createFromFormat('d-m-Y H:i', $date, 'Europe/Amsterdam')->toDateTimeString();
}
基本上,我在视图中close_date
放入的值会保存在数据库的start_date
中,close_date
设置为null。
知道如何将两个值保存在正确的位置吗?
答案 0 :(得分:2)
更改setCloseDateAttribute
功能:
public function setCloseDateAttribute($date)
{
$this->attributes['close_date'] = Carbon::createFromFormat('d-m-Y H:i', $date, 'Europe/Amsterdam')->toDateTimeString();
}
目前它还会写入$this->attributes['start_date']