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.8] Make updateExistingPivot() safe on non-existent pivot #29362

Merged
merged 1 commit into from Aug 1, 2019

Conversation

@mpyw
Copy link
Contributor

commented Aug 1, 2019

Fixes issue from [5.8] Fix many to many sync results with custom pivot model by themsaid · Pull Request #28416 · laravel/framework.

Currently, when we use updateExistingPivot() with a custom pivot class, the following code will be executed.

$updated = $this->getCurrentlyAttachedPivots()
    ->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
    ->where($this->relatedPivotKey, $this->parseId($id))
    ->first()
    ->fill($attributes)
    ->isDirty();

However, it is unsafe; If we call updateExistingPivot() for non-existent record, it will unexpectedly cause FatalThrowableError.

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function fill() on null
  File "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php", line xxx

This PR resolves this issue by fixing like this:

$pivot = $this->getCurrentlyAttachedPivots()
    ->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
    ->where($this->relatedPivotKey, $this->parseId($id))
    ->first();

$updated = $pivot ? $pivot->fill($attributes)->isDitry() : false;

@mpyw mpyw marked this pull request as ready for review Aug 1, 2019

@taylorotwell taylorotwell merged commit cc1b8f9 into laravel:5.8 Aug 1, 2019

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@mpyw mpyw deleted the mpyw:fix-update-existing-pivot-error-on-non-existent-record branch Aug 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.