Laravel Eloquent toArray / toJson with Carbon Date

时间:2015-02-10 12:26:35

标签: php laravel serialization laravel-4 php-carbon

我的模型的日期声明为Carbon

public $dates = array('created_at', 'submitted_at');

当我尝试使用->toArray()将模型转换为数组或创建json响应时,我收到错误:

[2015-02-10 12:25:02] local.ERROR: exception 'InvalidArgumentException' with message 'Data missing' in /var/www/public/app.orders/vendor/nesbot/carbon/src/Carbon/Carbon.php:385
Stack trace:
#0 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2671): Carbon\Carbon::createFromFormat('Y-m-d H:i:s.000', '2015-02-09 16:0...')
#1 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2276): Illuminate\Database\Eloquent\Model->asDateTime('2015-02-09 16:0...')
#2 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2255): Illuminate\Database\Eloquent\Model->attributesToArray()
#3 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Support/Collection.php(707): Illuminate\Database\Eloquent\Model->toArray()
#4 [internal function]: Illuminate\Support\Collection->Illuminate\Support\{closure}(Object(app\Entities\Order))
#5 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Support/Collection.php(709): array_map(Object(Closure), Array)
#6 /var/www/public/app.orders/app/controllers/OrdersController.php(49): Illuminate\Support\Collection->toArray()
#7 [internal function]: OrdersController->index('37')
#8 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231): call_user_func_array(Array, Array)
#9 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(93): Illuminate\Routing\Controller->callAction('index', Array)
#10 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(62): Illuminate\Routing\ControllerDispatcher->call(Object(OrdersController), Object(Illuminate\Routing\Route), 'index')
#11 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/Router.php(962): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'OrdersControlle...', 'index')
#12 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}('37')
#13 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/Route.php(109): call_user_func_array(Object(Closure), Array)
#14 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1028): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#15 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Routing/Router.php(996): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#16 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(775): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#17 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(745): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#18 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Session/Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#19 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#20 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#21 /var/www/public/app.orders/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#22 /var/www/public/app.orders/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(641): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#23 /var/www/public/app.orders/public/index.php(49): Illuminate\Foundation\Application->run()
#24 {main} [] []

如果我删除了DateMutator,一切顺利,但我想保留它,因为检索到的MSSQL日期并不是真正的用户友好。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我只找到了解决方法。似乎toArray函数调用以下内容:

Carbon\Carbon::createFromFormat('Y-m-d H:i:s.000', ...

在您的版本和我的版本中:

Carbon\Carbon::createFromFormat('Y-m-d H:i:s', ...

并且在某种程度上并非所有请求的数据(年/月......分钟/秒)都在对象中提供。

所以我所做的是在模型事件中,将模型带到或存储到数据库中,我手动修复日期,假设其余代码没有提供"无效"这些日期的值。当然,如果在某些时候为这些日期提供无效值并在保存到数据库之前调用toArray方法,则问题仍然存在。

public static function boot()
{
    parent::boot();

    static::saving(function($record){
        if (empty($record->the_date_attribute)) {
            // Perhaps set a default value here
        } else {
            if($record->the_date_attribute instanceof \Carbon\Carbon)
                $record->the_date_attribute->setTime(0,0,0);
        }
     }
    static::restored(function($record){
        if (empty($record->the_date_attribute)) {
            // Perhaps set a default value here
        } else {
            if($record->the_date_attribute instanceof \Carbon\Carbon)
                $record->the_date_attribute->setTime(0,0,0);
        }
     }
}

在我的情况下,我将时间设置为0,因为在我的数据库中,该字段是日期类型。 另一种可能的解决方法是覆盖toArray方法并为其提供额外的数据,但我更喜欢事件,因为DB的数据与我需要的值一起存储。

在您的情况下,解决方案可能更简单。也许你提供时间信息,但你没有提供秒......?

答案 1 :(得分:0)

MY2C

我通过在使用SqlServer

的每个模型中添加微秒来格式化定义来解决

protected $dateFormat = 'Y-m-d H:i:s.u';

我想知道是否有办法将其配置为每个模型的默认值