我无法使用MongoDB访问Laravel模型中的created_at
和updated_at
属性。我在数据库中的日期格式如下:
"created_at" : {
"sec" : NumberInt(1475126325),
"usec" : NumberInt(840000)
},
"updated_at" : {
"sec" : NumberInt(1475126325),
"usec" : NumberInt(840000)
},
我从MongoDB查询中得到以下结果:
App\Modules\Admin\Models\Segment Object
(
[table:protected] => tbl_segment
[timestamps] => 1
[fillable:protected] => Array
(
[0] => name
[1] => created_at
[2] => updated_at
)
[collection:protected] =>
[primaryKey:protected] => _id
[parentRelation:protected] =>
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[attributes:protected] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 57ecbefe15285745ba039871
)
[created_at] => Array
(
[sec] => 1475133182
[usec] => 832000
)
[updated_at] => Array
(
[sec] => 1475133182
[usec] => 832000
)
)
[original:protected] => Array
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 57ecbefe15285745ba039871
)
[created_at] => Array
(
[sec] => 1475133182
[usec] => 832000
)
[updated_at] => Array
(
[sec] => 1475133182
[usec] => 832000
)
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
当我尝试访问created_at
日期时,我收到以下错误:
Model.php第2983行中的ErrorException: preg_match()期望参数2为字符串,给定数组
答案 0 :(得分:0)
在为mongodb更新laravel包(jessengers)后,我遇到了类似的问题。你可以尝试 - 查找以下行
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
在 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
中找到的Model.php中的并将其替换为
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value['sec'])) {
vendor/nesbot/carbon/src/Carbon/Carbon.php
将 createFromFormat()
替换为 -
public static function createFromFormat($format, $time, $tz = null)
{
$time = date('Y-m-d H:i:s' , $time['sec']);
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
if ($dt instanceof DateTime) {
return static::instance($dt);
}
$errors = static::getLastErrors();
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
注意: - 可能有更好的解决方案,所以如果有其他人找到了替代方案,请分享。