Can not perform multiple sync() on single model instrance for morphToMany relationship. On second sync is thrown BadMethodCallException : Call to undefined method Illuminate\Database\Eloquent\Relations\Pivot::setMorphType()
The problem has occurred in the version 6.18.17. The version 6.18.16 works fine.
1) create model with morphToMany relationship
public function settings()
{
return $this->morphToMany(Setting::class, 'has_setting');
}
2) Sync multiple times in one request
$model = factory(Model::class)->create();
// Works
$model->tags()->sync([1]);
// Throws BadMethodCallException : Call to undefined method Illuminate\Database\Eloquent\Relations\Pivot::setMorphType()
$model->tags()->sync([2]);
Can confirm this is an issue. This appears to be the commit that broke:
https://github.com/laravel/framework/commit/684208b10460b49fa34354cc42f33b9b7135814f#diff-3c8f4b63b093b84a3e3d4e862743dec0
When $record is an instance of Illuminate\Database\Eloquent\Relations\Pivot, setMorphType is not an available method.
Edit: for the record, we are not saving twice, just once and are experiencing this problem.
I have the same problem, however in version 7.14. In version 7.13. * Did not present this problem.
Also experiencing this with 7.14.0, 7.13.0 works fine. Same commit to blame. as reported by @ralphschindler
For me the bug is showing when using spatie laravel-permission. In a test I ssign roles in two separate calls and I get this error.
This line in spatie laravel-permission triggers the issue:
Can confirm this issue in 7.14.
Will release a patch.
Most helpful comment
Will release a patch.