Laravel Advanced - Has One Through Relationship

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to lara tips in today's video we'll be talking about has one through relationship in laravel if you don't know about has one relationship then please go ahead and watch my tutorial on has one relationship this has one through relationship is a little bit related with has one relationship so if you don't know about that then please watch my video tutorial on that in has one relationship there will be two tables which are linked to each other by one to one relationship but in the hash one through relationship there will be three tables and they will be linked to each other by one to one relationship as well so i will be taking the same example from the larger document so you can see here the mechanics will be related to the car by has one relationship and car will be related to the owner by has one relationship so let's see that in the er diagram here so like this so as per the larval documentation a mechanic can repair only one car and a car has only one owner so in this scenario we we can use this has one through relationship so we can see here if we use has one relationship then we can get car from the mechanics and we can get owner from the car but all of these three are linked to each other so owner is linked to the mechanics through car so what we can say is mechanics has one owner through car okay so like this we can use has one through relationship so let's see that in action first we'll be using has one relationship and after that we'll be using has one through relationship directly so let's get started so what i have done previously is i have already created all the tables related to these things so it's i have created a mechanics model where you can see here id name and time stamp so all these fields for the mechanics i have created the migration for this and cars and owners as well so you can see here the car has the mechanics id as a foreign key and owner has the car id as a foreign key so you can see here the car migration and the owner migration file and also the models for mechanics so you can see the plane model or everything the car and the owner so all these are created and also if we see here in the database there are five mechanics and each mechanic has a car so mechanic can repair one car and if we see here in the owner's table each car is related to a single owner yeah and some of the owners have phone numbers and some of them do not have the phone number okay now let's go ahead and define the has one relationship on each one so what we can do is we can let's go to the mechanics table and mechanics is related to a car yeah so mechanic has a car so what we can do is puv public function uh scar we are just defining here has one relationship class here yeah so a mechanic can repair one car so we can create them with has one relationship and if you go inside the car class so a car has a owner so what we can do is owner return this has one owner class okay now we have this relationship here and in the owner we don't need to define any relationship because it is not related to anyone but it has one relationship now if we go here in the tinker well so what we can do is let us find a mechanic with the id of one mechanic find the id of one so if i execute this code then i'll get the mechanic so now if i go in the mechanics model so we can get the car from here yeah scar like this so if i execute this code so i am getting the car that the mechanic can repair so from the car so it's related to the its owner so if i go here in the in the car model here and you can see here there is the owner relationship so from the car i can access owner relationship and i can get the owner of the car yeah so if we do not define the has one through relationship then we have to do it like this to get the owner but if we define has one relationship we can directly get the owner from here like this so if we do it like this then we will get the owner so currently the relationship is not defined so we are getting null so let's define the relationship in the mechanics model so now let me go to the mechanic model and let me define the relationship the relationship name let's say o w n er owner and we have to return this has one true now here okay now we can say the mechanic has one owner class through car class okay so what we can say here a mechanic can a mechanic has one owner through car model okay so if we define a relationship like this then if we go here in the tinker well and i if i execute this code now i should get the owner here so if you see here the owner name is athena and if i go here as if i take the relationship like this as owner i should get the same thing here so if i execute this code so you can see here i am getting the same thing so using the has one relationship reduces the one layer of relationship usage okay now if we see here in this relationship definition here has one through relationship here are some conventions that we need to know okay so let me separate them in the separate line okay now by default what laravel assumes as in the third parameter it will assume here the foreign key in the this car's table so if i go here in the cars migration so what is that this mechanic idea so it will assume this as a mechanic id here and how it will assume it will take this from the this model name so it will convert it into the small letter and it will convert it into the snake case and it will suffix underscore id here and it will assume mechanic id here and here so it will assume the foreign key from the owner's table so if i go here in the owner's table so what's the foreign key it's a car id so it will assume the car id so how will it assume the car id so it will take this model and it will convert again it will convert it into the small letters and into the snake case and suffix underscore id there so these things will be assumed automatically by the rml so now if we go here in the cars migration here so let's say we don't want to call it here the mechanics we want to call the repairer repairer id then we have to write here the repairer id because if we do not give anything here if you do not pass anything here laravel will automatically assume this from this model name as a mechanic id in the third parameter what we have here the repairer id and it will not work so if we don't want to follow the convention or let's say we already have the tables from our previous project that can also be one case so in that case we can give the foreign key name here which should match the foreign key id in the cards table and also the car id should be the foreign id that should match the foreign id in the owner's table okay so apparently we are following the convention so we don't need to pass any parameters there okay now we have seen this now let's see this in the browser okay for that i have to run the server like this and let me go to the home controller here as always so here i have defined the mechanics key here and i have got all the mechanics here okay and i have passed that data in the welcome welcome blade and if we go in the welcome blade i have looped through all the mechanics and here and i have got this the owner so i am accessing this owner from the has one through relationship and i am getting the owner name and owner phone number here if the owner doesn't have phone number we are saying not available here now if i go here in the browser by clicking on this link now you can see here it is showing me all these here so this is the mechanic name all in the board later and this is the car owner and the phone number is here so these two have phone number these two do not have phone number and the last one has a phone number so we can access it like this so as we have seen in our previous lesson so it here also we are getting n plus one query problem so if i if i show you here so it is getting all the mechanics here and getting the owners information from each mechanic yeah it is running the mysql query on each mechanics and getting the owner's information so which results into the n plus one query problem so as always we can fix this by using either eager loading or lazy loading so if i go here in the home controller so what we can do is here with we can say here owner yeah now if i go here in the browser so you can see here the number of queries is 6 here now if i refresh here so you can see here the number of queries has been decreased to 2 and the query look like this so we have increased the performance of our application and now we can use also the lazy load uh from here so which will look like this so let me show you here so it will be like this owner so you the you would use this lazy loading let's say you have already got this mechanics collection from anywhere any other part of your application then in that case this lazy loading would be very handy so if i do it like this and again come here and refresh the page so you can see here the number of queries is again two so n plus one query problem is solved okay now let's see so we can also use where has where it doesn't have those kind of queries in this has one through relationship so let's see how to do that okay now let us show those car owners who have repaired their car with this mechanic and all those owners that have the phone number so if they do not have the phone number then let us not show them here in the list now what we can do here is we can say here where has like this and we can give the name of the relations if you are owning here like this and we can pass the closure in the second parameter and we'll get a query builder here as a parameter which is the query builder of the owner model and we can pass the query builder that is related to the model here so we can say where not null phone so this is the phone this is the column name and of the phone number in the owner's table so if we say here so we'll only be getting all the owners that have the phone number only so if i go here so currently we are seeing the five owners here so all this so this this and this two owners do not have any phone number so now we should not cbm here so if i replace here so look here we are only seeing these three results and all these owners have the phone number now also what we can do is we can only show those owners that don't have any phone number so what we can do here is where it doesn't have like this and if i go ahead and here refresh the base now you can see here we are getting all the owners that have repaired the cars with this mechanic and they don't have the phone number so this is how we use the has one through relationship in laravel so that's it for this video guys thank you for watching if you want to see more videos like this then please don't forget hit that thumbs up button and also hit subscribe button thank you for watching have a great day bye
Info
Channel: Laratips
Views: 1,123
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel relationship, has one through, has one through relationship, laravel advanced, laravel has on through, laravel 8 advanced, laravel advanced tutorial, laravel 8 advanced tutorial, laravel tutorial, laravel tutorial for beginners step by step, laratips
Id: s_fr5K39HAI
Channel Id: undefined
Length: 13min 20sec (800 seconds)
Published: Tue Oct 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.