I'm new to laravel, I want to delete the curriculum, it will delet the grades associated with it, and the subjcts that are associated with that grades

Viewed 35

These are the screenshots of Curriculum,Grade and subject table, Kindly someone help me code this problem in laravel.

Curriculum Table

Grade Table

Subject Table

1 Answers

Thanks, everyone I have found the answer to my question, by myself.

public function delete(Request $request)
    {
        // todo: delete subjects and grades that fall under this curriculum
        $grades = Grade::where('curriculumId', $request->curriculumId)->pluck('id')->all();
        $subjects = Subject::where('gradeId', $grades)->pluck('id')->all();
        $sujectCount = Subject::destroy($subjects);
        $gradeCount = Grade::destroy($grades);
        $curriculumCount = Curriculum::destroy($request->curriculumId);

        return [
            'status' => 'success',
            'deletedCurriculums' => $curriculumCount,
            'deletedGrades' => $gradeCount,
            'deletedSubjects' => $sujectCount,
        ];
    }
Related