{"id":15286,"date":"2020-06-23T13:39:33","date_gmt":"2020-06-23T13:39:33","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15286"},"modified":"2021-10-30T10:46:48","modified_gmt":"2021-10-30T10:46:48","slug":"how-to-delete-data-in-multiple-tables-with-relationship-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-delete-data-in-multiple-tables-with-relationship-laravel\/","title":{"rendered":"How to Delete data in Multiple Tables (with Relationship) Laravel."},"content":{"rendered":"\n<p class=\"has-very-light-gray-background-color has-background\">To delete data from database with with relationship with two tables in Laravel we have to use the solution that I&#8217;ve used. <\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\"><span class=\"has-inline-color has-vivid-red-color\">Now, Question is Where and when we use this Method ?<\/span><\/p>\n\n\n\n<p>Lets Discuss, what problems that i faced and how Solved it.<\/p>\n\n\n\n<p>In my project, I&#8217;ve two Tables in Database 1st is my <span class=\"has-inline-color has-vivid-red-color\">Users<\/span> table where users data is stored. <strong>See the below image :-<\/strong> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"305\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/users_table-1024x305.png\" alt=\"\" class=\"wp-image-15294\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/users_table-1024x305.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/users_table-300x89.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/users_table-768x229.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/users_table.png 1095w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>and 2nd is <span class=\"has-inline-color has-vivid-red-color\">SocialProviders<\/span> where users <span class=\"has-inline-color has-vivid-red-color\">Provider ID<\/span> 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. <strong>See the below image :-<\/strong> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"789\" height=\"179\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/Social_providers_table-.png\" alt=\"\" class=\"wp-image-15298\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/Social_providers_table-.png 789w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/Social_providers_table--300x68.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/Social_providers_table--768x174.png 768w\" sizes=\"auto, (max-width: 789px) 100vw, 789px\" \/><\/figure>\n\n\n\n<p>and whenever I Delete a user then I have to delete User data from both <span class=\"has-inline-color has-vivid-red-color\">Users<\/span> table and <span class=\"has-inline-color has-vivid-red-color\">SocialProviders<\/span> table for that user. <\/p>\n\n\n\n<p>Now, My problem is that I am not able to delete the <span class=\"has-inline-color has-vivid-red-color\">User <\/span>data from <strong>SocialProviders <\/strong>table but users table data is deleted and when the user re-login with the same Email ID then an error occurs. <\/p>\n\n\n\n<p>So, I&#8217;ve used a function in my delete function to delete the data from both the table when I delete the User. <strong>See the Below Code of Mine :- <\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/d75ed668e3220e9601feee3eb6d8478f.js\"><\/script>\n\n\n\n<p>Now, Understand What is written in the code and how it&#8217;s work. <\/p>\n\n\n\n<p>I&#8217;m Providing the image of code to explain better<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"899\" height=\"195\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/DeleteFunction.png\" alt=\"\" class=\"wp-image-15324\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/DeleteFunction.png 899w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/DeleteFunction-300x65.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/DeleteFunction-768x167.png 768w\" sizes=\"auto, (max-width: 899px) 100vw, 899px\" \/><\/figure>\n\n\n\n<p>In my users table, I&#8217;ve used <span class=\"has-inline-color has-vivid-red-color\">leftJoin<\/span> 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&#8217;ve called the destroy function. So it will delete the data from both the tables for that user. <\/p>\n\n\n\n<p>I hope it will help you and if you want to learn about Join method then go to Official Source from <span class=\"has-inline-color has-vivid-red-color\"><a href=\"https:\/\/laravel.com\/docs\/7.x\/queries#joins\" target=\"_blank\" rel=\"noopener\">Here<\/a><\/span> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Thanks ??<\/h2>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_19670\"><iframe loading=\"lazy\"  id=\"_ytid_59164\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_19670\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>To delete data from database with with relationship with two tables in Laravel we have to use the solution that I&#8217;ve used. Now, Question is Where and when we use&#8230; <\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201],"tags":[],"class_list":["post-15286","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15286"}],"version-history":[{"count":19,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15286\/revisions"}],"predecessor-version":[{"id":24724,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15286\/revisions\/24724"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}