Deleted method in Observer not get called on pivot array sync

Viewed 84

I have two tables Content and Languages and a Pivot table Content Languages. When I edit the content by attaching language Ids I used sync() (It has Id and a pivot column in $syncData). Also, have an ContentLanguageObserver to trigger some events at created and deleted events. But, only the created event is getting called on sync() and deleted method is never get called on deleting the value from content, but the DB is got updated.

I have several other related fields too, they're working fine on both events as they don't have a pivot array to updated at sync().

A working relationship in my code base:

App/Content.php

 public function genres(){

    return $this->belongsToMany(Genre::class, 'content_genres')->using('App\ContentGenre')->withTimestamps();
}

Sync function ContentService.php, where $genres is an array of ids only, this triggers created and deleted in the ContentGenreObserver.php

  $content->genres()->sync($genres);

Issue with Lanuguages:

public function audioLanguage() {
    
    return $this->belongsToMany(Language::class, 'content_languages')->select('languages.id','languages.english_name')->wherePivot('language_type', '=', Language::AudioLanguage)->using(ContentLanguage::class)->withTimestamps();
}

Sync function ContentService.php, where $syncData is an array of ids and pivot column, this triggers only created but not deleted in the ContentLanguageObserver.php

 $content->audioLanguage()->sync($syncData);
0 Answers
Related