Before updating/creating you can check the condition in a boot method inside a model file
for example - you have to check with the condition whether the date is empty or not before calling carbon function.
Let me know if you face any issue
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a Contract model that has date_start and date_stop fields. If I know the duration of the contract the date_stop will be filled otherwise not.
I use HTML 5 date fields in form and if the date_stop is not filled it returns ''.
I also use protected $dates = ['date_start', 'date_stop', ....]; in the model.
If I try to create the Contract model I receive an error from Carbon that tries to parse my null date:
InvalidArgumentException in Carbon.php line 414: Data missing in Carbon.php line 414 at Carbon::createFromFormat('Y-m-d H:i:s', '') in Model.php line 2959 at Model->asDateTime('') in Model.php line 2479 at Model->attributesToArray() in Model.php line 2456 at Model->toArray() in AppServiceProvider.php line 25 at AppServiceProvider->App\Providers{closure}(object(Contract)) at call_user_func_array(object(Closure), array(object(Contract))) in Dispatcher.php line 221 at Dispatcher->fire('eloquent.saved: App\Contract', array(object(Contract))) in Model.php line 1694 at Model->fireModelEvent('eloquent.saved: App\Contract', false) in Model.php line 1531 at Model->finishSave(array()) in Model.php line 1517 at Model->save() in Model.php line 546 at Model::create(array('_token' => 'buE5IDxOFzs0lrSmJ9bCJescJhXdM6nSuSP2KPAp', 'angajat_id' => '1993', 'data_contract' => '2017-01-30', 'firma_id' => '7', 'echipa_id' => '54', 'data_inceput' => '2017-01-30', 'perioada' => 'nedeterminata', 'data_sfarsit' => '', 'data_incetare' => '', 'ocupatie_id' => '1', 'ore_sapt' => '35', 'zile_sapt' => '5', 'zile_co' => '40', 'data_efecte' => '2017-01-30', 'save' => 'Save')) in AngajatiContracteController.php line 62 at AngajatiContracteController->store(object(Request), '{angajati}')
Is there a solution for this or I shoud initialize the null dates from forms before Contract::create(...)?
Thank you!
Please or to participate in this conversation.