我的MySQL数据库中有TIME
类型的'timeAfter'列,使用QueryBuilder
$table->time('timeAfter');
创建,我已将以下内容添加到我的模型中:
public $timestamps = false;
public function getDates () {
return array ('timeAfter');
}
但是当我运行我的应用时出现以下错误,Date Mutator似乎需要DATE
或DATETIME
而不是TIME
列。任何想法?
InvalidArgumentException
Unexpected data found. Unexpected data found. Data missing
open:
...\vendor\nesbot\carbon\Carbon\Carbon.php
}
if ($dt instanceof \DateTime) {
return self::instance($dt);
}
$errors = \DateTime::getLastErrors();
throw new \InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
public static function createFromTimestamp($timestamp, $tz = null)
答案 0 :(得分:4)
我刚遇到同样的问题。我Raphael_的笔记并创建了一个变异器,它工作得很好。 Carbon解析了TIME列就好了。
public function getTimeAfterAttribute($value) {
return Carbon::parse($value);
}
答案 1 :(得分:1)
就我而言,@ normany回答中使用的Carbon::parse()
返回了一个例外:
Exception with message 'DateTime::__construct(): Failed to parse time string (50:00:00) at position 0 (5): Unexpected character'
所以我不得不把它改成
return Carbon::createFromFormat('H:i:s', $value);
但是,在我的情况下,TIME
列可以包含大于24的小时。对于'50:00:00'
,返回的值是后天,加上2小时,这不是我真正需要的。
因此,我更改了getTimeAfterAttribute()
方法,如下所示:
public function getTimeAfterAttribute($value)
{
list($hours, $minutes, $seconds) = explode(':', $value);
return CarbonInterval::create(null, null, null, null, $hours, $minutes, $seconds);
}
这样,使用'50:00:00'
,我得到var_dump()
输出:
class Carbon\CarbonInterval#921 (15) {
public $y =>
int(0)
public $m =>
int(0)
public $d =>
int(0)
public $h =>
int(50)
public $i =>
int(0)
public $s =>
int(0)
public $weekday =>
int(0)
public $weekday_behavior =>
int(0)
public $first_last_day_of =>
int(0)
public $invert =>
int(0)
public $days =>
bool(false)
public $special_type =>
int(0)
public $special_amount =>
int(0)
public $have_weekday_relative =>
int(0)
public $have_special_relative =>
int(0)
}