-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The $dateFormat protected property is not respected on pivot models #22484
Comments
How are you storing dates in the database? Using Laravel's default I made a simple demo and managed to get the same error as you, Carbon's moaning because the actual date in the DB doesn't conform to the format you're trying to format it as. |
I'm storing dates with microseconds (2018-01-05 13:53:03.123456) |
As a timestamp field? |
Yeah, my migrations look like this for the field:
This creates a timestamp field in Postgres with microsecond precision. All my models have a |
Gotcha. Does all your data in your database conform to that? I've filled up a few tables with just dates like I also notice that using the |
Yeah, it does. The problem explicitly seems to occur when you attach via a pivot using |
I've tracked down the issue. There's a trait that deals with insert records into the pivot table When using |
Awesome, I had tried to track down the root cause and I figured it was something like that, but got lost in the depths of the framework. Appreciate the fix! |
Description:
When using a model to represent a pivot relationship, the
protected $dateFormat
property is not respected for timestamps on the pivot model.Steps To Reproduce:
Create two tables (in this case, accounts and services.) Create a pivot table between them (account_service.) Attach as a belongsToMany relationship as below:
return $this->belongsToMany(Service::class) ->using(AccountService::class) ->withTimestamps();
On the pivot model, set
protected $dateFormat = "Y-m-d H:i:s.u
. Attaching models will now cause a Carbon error, started via this stack trace:InvalidArgumentException : Data missing /usr/share/sonar/vendor/nesbot/carbon/src/Carbon/Carbon.php:582 /usr/share/sonar/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:715
However, creating a pivot model directly and setting the account ID and service ID on it will save correctly with the appropriate microsecond precision timestamps.
The 'getDateFormat()' call on line 716 of HasAttributes.php does return the expected
$dateFormat
set on the pivot model. However, the$value
passed in is a standard YYYY-MM-DD HH:mm:ss value.The text was updated successfully, but these errors were encountered: