Framework: Can not perform multiple sync() on morphToMany relationship

Created on 2 Jun 2020  路  5Comments  路  Source: laravel/framework


  • Laravel Version: 6.18.17
  • PHP Version: 7.2.24
  • Database Driver & Version: MariaDB 10.0

Description:

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.

Steps To Reproduce:

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]); 

Most helpful comment

Will release a patch.

All 5 comments

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:

https://github.com/spatie/laravel-permission/blob/e25986c298b921e884a0a93a6f09f81c0c9cdb5a/src/Traits/HasRoles.php#L116

Can confirm this issue in 7.14.

Will release a patch.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

kerbylav picture kerbylav  路  3Comments

progmars picture progmars  路  3Comments

shopblocks picture shopblocks  路  3Comments