Abdul Rehman
Abdul Rehman

Reputation: 159

How to delete a foreign key constraint in Laravel?

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.

Upvotes: 4

Views: 20797

Answers (3)

Bubu23
Bubu23

Reputation: 31

In Laravel 9 you can use detach method:

$user->registrations()->detach();
$user->delete();

Upvotes: 1

Jean Paul CP
Jean Paul CP

Reputation: 253

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();

Upvotes: 4

Abdul Rehman
Abdul Rehman

Reputation: 159

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

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

Upvotes: 4

Related Questions