Framework: [5.8] Custom pivot class causes updated_at not to be updated

Created on 29 Jul 2019  路  14Comments  路  Source: laravel/framework

  • Laravel Version: 5.8.29
  • PHP Version: 7.2.19
  • Database Driver & Version: mysql 5.7

Description:

Hello,

we have an issue when using a custom pivot class, with an additional pivot attribute. The updated_at field is not updated, while the value of the additional field is updated, when using sync() method on the belongsToMany relation. We tried to add $timestamps = true to the custom pivot class but it's not working.

Let's say for example, that we have a users table, a roles tables, and a users_has_roles pivot table with a additional comment field.

If we sync the relation without a call to using(), so without custom pivot class, it's working fine, the comment is updated, and the updated_at field too.

But if we add using() to our relation method, so with custom pivot class, it's not working fully,
the comment is updated, but the updated_at field is not.

Steps To Reproduce:

1 / clone https://github.com/lk77/pivot-using-update-bug
2 / configure .env and execute php artisan migrate --seed
3 / php artisan serve
4 / go to localhost:8000
5 / See that the two dates differs (one is calling using() the other is not)

thanks.

bug

Most helpful comment

@mpyw i pulled your fork and updated my demo : https://github.com/lk77/pivot-using-update-bug
the issue is fixed :

"Fri Aug 02 2019 06:58:09 GMT+0000"
"Fri Aug 02 2019 06:58:09 GMT+0000"

the dates are equal.

thanks

All 14 comments

This was caused by #27571. /cc @ralphschindler

Any news about that issue ?

I am trying to write a test for this behavior. Standby.

PR was merged.

@driesvints Please reopen, the linked PR is an old one. This issue hasn't been fixed yet.

Earlier, i was thinking that this pr (28416) may be related to my issue, and commented,
but since it's not the case, i clarified it on the pr and linked to this issue,
but it's not related.

@staudenmeir I meant this PR: https://github.com/laravel/framework/pull/29362

@driesvints That's also a different issue ;-)

Okay lol

@driesvints Sorry for confusing mention

Very ditry hack:

 $this->newPivot([
     $this->foreignPivotKey => $this->parent->{$this->parentKey},
     $this->relatedPivotKey => $this->parseId($id),
+    (new $this->using)->getCreatedAtColumn() => null,
 ], true)->fill($attributes)->save();

It forces AsPivot::hasTimestampAttributes() to return true.

Okay I got the better implementation, I'll submit PR

@lk77 Would you test it based on this PR branch?

@mpyw i pulled your fork and updated my demo : https://github.com/lk77/pivot-using-update-bug
the issue is fixed :

"Fri Aug 02 2019 06:58:09 GMT+0000"
"Fri Aug 02 2019 06:58:09 GMT+0000"

the dates are equal.

thanks

Was this page helpful?
0 / 5 - 0 ratings