0

I have three tables student, user and contact, Student has a relation with Contact and Contact has a relation (user_id) with the User table. When I try to delete from the Student, it should be removed from User and Contact using cascade delete. However, it deletes from the Student table only and keeps data in the User and Contact tables.

StudentController

<?php

public function destroy(Student $student)
{
    $student->delete();

    return redirect('/admin/student')->with('success','has been Deleted');
}

I used in student model

  public function contact()
{
    return $this->belongsTo('App\Contact');

}

public static function boot() {
    parent::boot();

    static::deleting(function($student) {
         $student->contact()->delete();

    });

it is worked but I used in contact model

  public function user()
{
    return $this->belongsTo('App\User');

}
public static function boot() {
    parent::boot();

    static::deleting(function($contact) {
         $contact->user()->delete();

    });

in delete student delete contact but not delete user

I have no idea how to fix it.

0 Answers0