Skip to content
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

[5.5] Ensure Pivot models $dateFormat is used when creating a pivot record. #22656

Closed
wants to merge 2 commits into from
Closed

Conversation

kevinruscoe
Copy link
Contributor

As with #22484 , when inserting a pivot record via ->attach() the InteractsWithPivotTable.php trait doesn't check to see if the pivot table has a model representation. It' simply finds the table name and inserts a new record. Because of this, the timestamps are passed as Carbon instances and then turn to strings via its toString() method. If the consumer is using a pivot Model, it should use that models dateFormat to format by, not the toString() method. This simply news-up the pivot model, fetches it's dateformat and formats the $fresh date against it.

As with #22484 , when inserting a pivot record via `->attach()` the `InteractsWithPivotTable.php` trait doesn't check to see if the pivot table has a model representation. It' simply finds the table name and inserts a new record. Because of this, the timestamps are passed as `Carbon` instances and then turn to strings via its `toString()` method. If the consumer is using a pivot Model, it should use that models dateFormat to format by, not the `toString()` method. This simply news-up the pivot model, fetches it's dateformat and formats the `$fresh` date against it.
@kevinruscoe
Copy link
Contributor Author

...actually, hold fire with this.

@kevinruscoe kevinruscoe closed this Jan 5, 2018
@kevinruscoe kevinruscoe reopened this Jan 6, 2018
@kevinruscoe
Copy link
Contributor Author

This won't actually fix the issue above, it does fix some sort of issue ;), in that the ->attach() method now actually adheres to what the Pivot model requests the $dateFormat to be.

@kevinruscoe kevinruscoe changed the title Update InteractsWithPivotTable.php [5.5] Ensure Pivot models $dateFormat is used when creating a pivot record. Jan 6, 2018
@taylorotwell
Copy link
Member

So it doesn't fix the issue you link? Need more information and explanation before merging.

@kevinruscoe
Copy link
Contributor Author

It's fixes part of the issue above, but the actual issue it fixes is as follows.

When using the ->attach() to relate many-to-many records, the trait doesn't check to see how the date should be stored. It simply find's the pivot table, checks if the timestamps are needed and then spins up 2 Carbon instances for the created_at and updated_at. When inserting these records into the DB the Carbon instances are cast as strings, so the toString() method is called and it converts them to Carbon's default (Y-m-d h:i:s).

This PR basically checks to see if the Pivot model has a $dateFormat specified, if it does it'll use them when adding the record to the DB, rather than the default toString() method.

@sisve
Copy link
Contributor

sisve commented Jan 6, 2018

This sounds like something that should have tests.

@taylorotwell
Copy link
Member

@sisve indeed.

@taylorotwell
Copy link
Member

@kevdotbadger You are calling ::getDateFormat() but I see no static method getDateFormat on any model class at all? Have you actually run this code?

@taylorotwell
Copy link
Member

I put this in 5.6 since it required a breaking change to method visibility of getDateFormat. I also added a test.

@brlocky
Copy link

brlocky commented Aug 24, 2022

I'm having this issue in Laravel 9 !
So basically when using the attach to store a relation, the datetime precision is not respected.
2022-08-24 23:21:39.000000

I have precision 6 on the timestamps and also the option ->withTimestamps();

@brlocky
Copy link

brlocky commented Aug 24, 2022

the issue seams to be fixed by adding the .u on the default time format, with a patch on Grammar.php

namespace Illuminate\Database;

     * Get the format for database stored dates.
     *
     * @return string
     */
    public function getDateFormat()
    {
        return 'Y-m-d H:i:s.u';
    }
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants