[Vuejs]-How to correctly delete items when relationships are involved?

1👍

You are calling delete on a Relationship object not an Eloquent Model:

$question->surveyQuestionOption()->delete();

This will end up calling the base Query Builder’s delete method in the end to do a delete query directly; it does not call the delete method on the Model, which is what fires the Model events.

SectionController@destroy:

$section = SurveySection::findOrFail($id);

$section->delete();

SurveySection@boot:

static::deleting(function ($section) {
    $section->surveyQuestions->each(function ($question) {
        $question->delete();
    });
});

SurveyQuestion@boot:

static::deleting(function ($question) {
    $question->surveyQuestionOptions->each(function ($option) {
        $option->delete();
    });
});
👤lagbox

Leave a comment