Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

seyyed_am7's avatar

Sync morph many relationship in Laravel

Hi I have 3 tables that they're in morphmany relationship and this is my models:

class Blog extends BasicModel
{
    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class , 'taggable' , 'taggables');
    }
}
class Tag extends BasicModel
{
    protected $fillable = ['name'];
    public function blogs(): MorphToMany
    {
        return $this->morphedByMany(Blog::class , 'taggable' , 'taggables');
    }
}

Is there any way to save tags and sync them by blog model in one line, like the original sync method??

0 likes
1 reply
bobbybouwmann's avatar

MorphToMany extends the BelongsToMany relationship so you should have the sync method available here as well.

$blog = Blog:::first();
$blog->tags()->sync([1, ,2, 3]);

Please or to participate in this conversation.