【发布时间】:2016-08-30 05:40:30
【问题描述】:
我想像这样发布合并的日期和时间戳('H:i')。
private function update($test, $request)
{
$test -> merge_date = $request -> date . " " . $request -> timestamp_hi;
$test->save();
}
日期值类似于“2016-08-30”, timestamp_hi 值类似于“12:45”。
我希望合并那里的值并将数据库记录设置为“2016-08-30 12:45:00”。
但这会给我带来错误。
InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Data missing
at Carbon::createFromFormat('Y-m-d H:i:s', '12:45:00') in Model.php line 2969
如果我将 timestamp_hi 值设置为 '2016-08-30 12:45:00',Carbon create 是这样的。
InvalidArgumentException in Carbon.php line 425:
The separation symbol could not be found
Unexpected data found.
Trailing data
at Carbon::createFromFormat('Y-m-d H:i:s', '2016-08-30 2016-08-30 12:45:00') in Model.php line 2969
看来我可以合并 2 个帖子,但不能处理时间戳('H:i')。为什么?
附言
模型改变了这一点。我之前没有设置受保护的 $dates 和 Attribute。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
protected $fillable = ['date', 'timestamp_hi'];
protected $dates = ['date', 'timestamp_hi'];
public function getDateAttribute($date)
{
return $this->attributes['date'] = \Carbon\Carbon::createFromFormat('Y-m-d', $date) -> toDateString();
}
public function getTimestampHiAttribute($timestamp_hi)
{
return $this->attributes['timestamp_hi'] = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $timestamp_hi) -> format('H:i');
}
}
【问题讨论】:
标签: php laravel-5.2 postgresql-9.5