0

I am having trouble deleting the relation model records, I want to keep some records and delete all others, for example, there is x number of records in the table now I want to keep the first five and delete all others. I am using skip and take to filter the records.

quiz->questions()->latest()->skip(request->total_questions)->take(PHP_INT_MAX)->each(function (row) {row->options()->delete();
    $row->delete();
});

I have a model quiz that has related questions and questions have other relation options so what I won’t do is keep some questions like 5 or 6 maybe and delete all other questions with their options but in this query, all questions related to the quiz are deleted.

P.s when I use to get() it shows me the correct result which I want but I don’t know why it is not working with delete()!

Please help anyone.

john Asked question May 13, 2021