碳日期转换仅在一个日期字段中失败

时间:2019-07-28 16:25:54

标签: laravel-5 php-carbon

在laravel 5.5应用程序上使用Carbon进行日期转换时,我面临一个奇怪的情况。 我有几个日期字段定义为mysql中的日期,默认为NULL。 mysql格式为“ Y-m-d”,但在前端形式中,我必须显示“ d / m / Y”格式。 Sequel Pro table screenshot

要实现这一点,我正在使用carbon通过模型更改器转换日期。 这意味着,首先我的日期为:

    protected $dates = [
    'valid_from',
    'valid_to',
    'provisional_reception',
    'final_recepcion',
    'policy_cancellation',
];

我的变种人采用以下方式:

public function setValidFromAttribute($input)
{
    if ($input != null && $input != '') {
        $this->attributes['valid_from'] = Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
    } else {
        $this->attributes['valid_from'] = null;
    }
}


public function getValidFromAttribute($input)
{
    if ($input != null) {
        return Carbon::createFromFormat('Y-m-d', $input)->format('d/m/Y');
    } else {
        return '';
    }
}


public function setValidToAttribute($input)
{
    if ($input != null && $input != '') {
        $this->attributes['valid_to'] = Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
    } else {
        $this->attributes['valid_to'] = null;
    }
}


public function getValidToAttribute($input)
{
    if ($input != null) {
        return Carbon::createFromFormat('Y-m-d', $input)->format('d/m/Y');
    } else {
        return '';
    }
}

public function setProvisionalReceptionAttribute($input)
{
    if ($input != null && $input != '') {
        $this->attributes['provisional_reception'] = Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
    } else {
        $this->attributes['provisional_reception'] = null;
    }
}

public function getProvisionalReceptionAttribute($input)
{
    if ( $input != null) {
        return Carbon::createFromFormat('Y-m-d', $input)->format('d/m/Y');
    } else {
        return '';
    }
}  
public function setFinalReceptionAttribute($input)
{
    if ($input != null && $input != '') {
        $this->attributes['final_reception'] = Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
    } else {
        $this->attributes['final_reception'] = null;
    }
}


public function getFinalReceptionAttribute($input)
{

    if ($input != null) {
        return Carbon::createFromFormat('Y-m-d', $input)->format('d/m/Y');
    } else {
        return '';
    }
} 


public function setPolicyCancellationAttribute($input)
{
    if ($input != null && $input != '') {
        $this->attributes['policy_cancellation'] = Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
    } else {
        $this->attributes['policy_cancellation'] = null;
    }
}


public function getPolicyCancellationAttribute($input)
{
    if ($input != null) {
        return Carbon::createFromFormat('Y-m-d', $input)->format('d/m/Y');
    } else {
        return '';
    }
}

现在,除了一个字段“ policy_cancellation”之外,一切都正常运行

将数据从表单插入数据库时​​,如果policy_cancellation为空,则提交成功,但是如果有数据,PDO将返回错误,例如:

valid_from  "01/01/2019"
valid_to "30/10/2019"
provisional_reception ""
final_recepcion ""
policy_cancellation "25/07/2019"

触发以下操作:

[2019-07-28 15:33:17] local.ERROR: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '25/07/2019' for column `database`.`insurance_policies_emitteds`.`policy_cancellation` at row 10586 (SQL: update `insurance_policies_emitteds` set `final_amount` = , `policy_cancellation` = 25/07/2019 where `policy_number` = 1234567889) {"userId":1,"email":"faridsilva@gmail.com","exception":"[object] (Illuminate\\Database\\QueryException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '25/07/2019' for column `database`.`insurance_policies_emitteds`.`policy_cancellation` at row 10586 (SQL: update `insurance_policies_emitteds` set `final_amount` = , `policy_cancellation` = 25/07/2019 where `policy_number` = 1234567889) at /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\\DBAL\\Driver\\PDOException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '25/07/2019' for column `database`.`insurance_policies_emitteds`.`policy_cancellation` at row 10586 at /Volumes/data/Sites/database/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119, PDOException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '25/07/2019' for column `database`.`insurance_policies_emitteds`.`policy_cancellation` at row 10586 at /Volumes/data/Sites/database/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117)
[stacktrace]
#0 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\\Database\\Connection->runQueryCallback('update `insuran...', Array, Object(Closure))
#1 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Connection.php(490): Illuminate\\Database\\Connection->run('update `insuran...', Array, Object(Closure))
#2 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Connection.php(423): Illuminate\\Database\\Connection->affectingStatement('update `insuran...', Array)
#3 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2173): Illuminate\\Database\\Connection->update('update `insuran...', Array)
#4 /Volumes/data/Sites/database/app/Observers/InsurancePoliciesEmittedObserver.php(16): Illuminate\\Database\\Query\\Builder->update(Array)
#5 [internal function]: App\\Observers\\InsurancePoliciesEmittedObserver->saved(Object(App\\Models\\InsurancePoliciesEmitted))
#6 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(369): call_user_func_array(Array, Array)
#7 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(200): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}('eloquent.saved:...', Array)
#8 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(173): Illuminate\\Events\\Dispatcher->dispatch('eloquent.saved:...', Array, false)
#9 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(148): Illuminate\\Events\\Dispatcher->fire('eloquent.saved:...', Object(App\\Models\\InsurancePoliciesEmitted))
#10 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(591): Illuminate\\Database\\Eloquent\\Model->fireModelEvent('saved', false)
#11 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(562): Illuminate\\Database\\Eloquent\\Model->finishSave(Array)
#12 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(755): Illuminate\\Database\\Eloquent\\Model->save()
#13 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Support/helpers.php(1038): Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}(Object(App\\Models\\InsurancePoliciesEmitted))
#14 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(756): tap(Object(App\\Models\\InsurancePoliciesEmitted), Object(Closure))
#15 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1477): Illuminate\\Database\\Eloquent\\Builder->create(Array)
#16 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1489): Illuminate\\Database\\Eloquent\\Model->__call('create', Array)
#17 /Volumes/data/Sites/database/app/Http/Controllers/Admin/InsurancePoliciesEmittedsController.php(63): Illuminate\\Database\\Eloquent\\Model::__callStatic('create', Array)
#18 [internal function]: App\\Http\\Controllers\\Admin\\InsurancePoliciesEmittedsController->store(Object(App\\Http\\Requests\\Admin\\StoreInsurancePoliciesEmittedsRequest))
#19 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#20 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('store', Array)
#21 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Admin\\InsurancePoliciesEmittedsController), 'store')
#22 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()
#23 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\\Routing\\Route->run()
#24 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#25 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#26 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#29 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(68): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#32 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#33 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#34 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#35 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#38 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#39 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#40 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#41 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#42 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#43 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#44 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#45 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#46 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#47 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Router.php(660): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#48 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#49 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Router.php(601): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#50 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Router.php(590): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#51 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#52 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#53 /Volumes/data/Sites/database/vendor/fideloper/proxy/src/TrustProxies.php(56): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#54 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#55 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#56 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#57 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#58 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#59 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#60 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#61 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#62 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#63 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#64 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#65 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#66 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#67 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#68 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#69 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#70 /Volumes/data/Sites/database/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#71 /Volumes/data/Sites/database/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#72 /Volumes/data/.composer/vendor/laravel/valet/server.php(158): require('/Volumes/data/S...')
#73 {main}
"} 

另一个奇怪的情况(至少对我来说)是在policy_cancellation mutator中插入一个中断,它显示了正确的格式化值

 public function setPolicyCancellationAttribute($input)
{
     if ($input != null && $input != '') {
        $this->attributes['policy_cancellation'] = \Carbon\Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
      dd($this->attributes['policy_cancellation']);  
    } else {
        $this->attributes['policy_cancellation'] = null;
    }
}

它返回“ 2019-07-25”,它应该是正确的日期转换。 但是,再次删除dd()会导致上一个错误。

我尝试使用普通的php省略该特定领域的碳:

public function setPolicyCancellationAttribute($input)
    {
         if ($input != null && $input != '') {
            $input = str_replace('/', '-', $input); // convert from d/m/Y to d-m-Y for disambiguation
            $input = strtotime($input);
            $input = date('Y-m-d',$input);
            $this->attributes['policy_cancellation'] = $input;
            //$this->attributes['policy_cancellation'] = \Carbon\Carbon::createFromFormat('d/m/Y', $input)->format('Y-m-d');
            //dd($this->attributes['policy_cancellation']);  
        } else {
            $this->attributes['policy_cancellation'] = null;
        }
    }

但是没有区别。 dd()显示正确的格式化值“ 2019-07-25”,然后mysql触发与前面所述相同的错误。

在这种情况下,我一直陷于困境,没有找到解决错误的替代方案,因此任何想法都会得到真正的赞赏。

1 个答案:

答案 0 :(得分:0)

您的SQL转储显示

`policy_cancellation` = 25/07/2019

1 /与您的dd()“ 2019-07-25”不匹配,它使用的是/而不是-的原始值

2 /应该加引号。

`policy_cancellation` = '2019-07-25'