MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Delete data in Multiple Tables (with Relationship) Laravel.

To delete data from database with with relationship with two tables in Laravel we have to use the solution that I’ve used.

Now, Question is Where and when we use this Method ?

Lets Discuss, what problems that i faced and how Solved it.

In my project, I’ve two Tables in Database 1st is my Users table where users data is stored. See the below image :-

PhpMyAdmin users table with id, role_id, name, email and hashed password columns

and 2nd is SocialProviders where users Provider ID is stored (whenever a user login with google, Facebook, etc then unique provider_id is stored to verify the users ) with user_id to find that which provider is for which user. See the below image :-

PhpMyAdmin social_providers table row linking user_id 11 to a Google provider_id

and whenever I Delete a user then I have to delete User data from both Users table and SocialProviders table for that user.

Now, My problem is that I am not able to delete the User data from SocialProviders table but users table data is deleted and when the user re-login with the same Email ID then an error occurs.

So, I’ve used a function in my delete function to delete the data from both the table when I delete the User. See the Below Code of Mine :-

Now, Understand What is written in the code and how it’s work.

I’m Providing the image of code to explain better

Laravel destroysm function using leftJoin to delete a user and their social_providers rows, with annotations

In my users table, I’ve used leftJoin method to declare that in social_providers table, users id is equal to social_providers user_id (relationship established between them), where users id is equal to the $id (Variable id) and variable id is passed in the destroysr function which returns the users id for which I’ve called the destroy function. So it will delete the data from both the tables for that user.

I hope it will help you and if you want to learn about Join method then go to Official Source from Here

Thanks ??

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More

What is Laravel? and Its Features.

In this Tutorial we are going to learn about Laravel. But most importantly you have to know about PHP first. If you don’t know about PHP and…

Read More

How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-2)

How to merge two or multiple tables to each other in the Laravel PHP Framework? Part-1 Part-3 Step 1. Go to app/Country.php. Now, we need to list…

Read More

Top 10 Basic Laravel Interview Questions.

Q:-1 What is Laravel? Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller…

Read More

CRUD Operation Using Model in Laravel.

Inserting data in Database through Model. (Click to Open) Read, Update, and Delete data through Model. (Click to Open) Sushant Kumar With MotoShare.in, you can book a…

Read More

How to Create Laravel Eloquent API Resources to convert model collections into JSON(Part 5).

Deepak Kumar MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in…

Read More