How to delete a foreign key constraint in Laravel?

10,702

Solution 1

 $table->integer('user_id')->unsigned();
 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Using ->onDelete('cascade') works for me :)

Solution 2

You cannot delete a record from the user table because you have a relationship with the registrations table (registrations_user_id_foreign). In order to delete a user record, you first have to delete all the records for that user from the registrations table. You have two ways:

Modify the relationships of your registrations table

$table->foreign('user_id')->references ('id')->on('users')->onDelete ('cascade');

With this, when you do a

$user->delete()

the records of that user will be deleted from the registrations table too.

Or second way:

Delete user after deleting registrations records

$user->registrations()->delete();
$user->delete();
Share:
10,702

Related videos on Youtube

Abdul Rehman
Author by

Abdul Rehman

My skills are not professional, but I have worked in Laravel, JSP, JSF, Hibernate and had worked for client side through component developing.

Updated on June 04, 2022

Comments

  • Abdul Rehman
    Abdul Rehman almost 2 years

    I'm working on a project in which I' m adding data in two tables User & Registration in one form like this

     public function storeStudent(Request $request)
    {
           $created_user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
            'parent_id' => $request->parent_id,
            'role_id' => $request->role_id,
            'gender'=> $request->gender,
            'date_of_birth'=> $request->date_of_birth,
            'cnic'=>$request->cnic,
            'religion'=>$request->religion,
            'skills'=>$request->skills,
            'physical'=>$request->physical,
            'emergency_name'=>$request->emergency_name,
            'phone_no'=>$request->phone_no,
            'medical_info'=>$request->medical_info,
            'family_dr'=>$request->family_dr,
            'address'=>$request->address,
    
    
        ]);
        Registration::create([
    
           //reg_id is id for registration table
    
            'user_id' => $created_user->id,
            'class_id' => $request->class_id,
            'section_id' => $request->section_id,
    
        ]);
    

    Now I want to delete the data I'm bad with syntaxes. I don't know what should I do to delete the data I m trying

    public function destroy(Request $request)
    {
    
        $user = User::findOrFail($request->user_id);
        $user->delete();
    
        $registration=Registration::findOrFail($request->user_id);
        $registration->delete();
        return back();
    
    }
    

    Table for Registration is

    Schema::create('registrations', function (Blueprint $table) {
        $table->increments('reg_id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('class_id')->unsigned();
        $table->foreign('class_id')->references('id')->on('classses');
        $table->integer('section_id')->unsigned();
        $table->foreign('section_id')->references('id')->on('sections');
        $table->timestamps();
    });
    

    But it gives me error

     Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`jurs1`.`registrations`, CONSTRAINT `registrations_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: delete from `users` where `id` = 10)
    

    Please help me in this case.

    • Saad
      Saad about 5 years
      Why are you using $request->user_id to delete Registration? Is your user_id and registration_id same?
    • Abdul Rehman
      Abdul Rehman about 5 years
      because I want to delete the row of that user_id, and reg_id is different
    • manishk
      manishk about 5 years
      Check this out- stackoverflow.com/questions/47544109/… (it uses onDelete('cascade'))