Laravel Advanced - Has Many 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 many true relationship if you also want to know about has one true relationship then i have already made a video about that so you can watch my previous video now let's talk about has many through relationship i will be taking the same example from the larval documentation here okay so here are three tables countries users and posts so let's see that in the er diagram so if i go here and just ignore all other these tables so we'll be only looking at these tables now so all the other tables are from our previous video tutorial now you can see here the table structure is same that we have look in our has one through relationship so from this what we can say is the table structure for the has one through and has many through relationship will be same but the difference is how we are going to implement that so the use case is different now let's say a country has many users and a user has many posts yeah so we can calculate this by using the has many relationship but now what we want is we want to get all the posts of a country directly now using the has many relationship we cannot directly get all the posts of our country we have to get the users and from the users we have to get the post but using the has many through relationship we can directly access the posts of a particular country so what we can say is a country has many posts through users so using the relationship in this way we can directly get all the posts of a country okay now let's see here in the code so i have already created the models and migrations for those so here is a country model and here is the user's model and by default laravel ships with the users model so there are all data here now all these data are from our previous video tutorial so let us just ignore this and also we have a post model here so it's just empty i have created a new one now if we see here in the countries table we have only name here field and in the users table all these data are from the default level migration and i have only added this country for an id here so a country can have many users so i have added this and we have a posts table so here you can see the user id is a foreign key here and we have a title and in each active field which is by default true now let us first create the has many relationship on each of those models and let's see how to access the countries from there and after that we'll look into the has many through relationship so in the country can have many users so return this has many user class and if we go here in the users model we can define a user can have many posts so let us define the relationship like this return this has many post class so what we have said is a country has many users and a user has many posts yeah so now if i go here in the tinker where let us find the country with the id one yeah also i have seated the data here so if you see here the three countries here it's uh these are the list of the three countries uh from where i am getting the most views so thank you guys also thank you for all those people who are watching from other countries as well so we have three countries and here in the users table we can see here each country has three users here and if i go here in the post table each user has two posts okay so here if we see here there are nine users so we are having here 18 posts okay now if i go here in the tinker well now if i find the country with the id1 so we can get like this so now to get the countries first we have to get the users so we have to get the users like this and here we have to loop let's say map and function here user so we have to return here it from user we have to return posts like this so now if i do it like this so you can see here i am getting all the posts but the posts are grouped into different collections so you can see here in one collection there are two posts and in this collection also there are two posts and in this collections also to post so we are not directly getting the posts so for that what we have to do is call flatten method here so if i call it like this so you can see here i am getting all the posts of a country so we can get it like this we are using this collection helper method this pattern is a collection helper method so using in this way there is a little bit performance here because we are getting the data and we are calculating it in the phpa so it will be a little bit slower using the data from directly from the mysql is much faster than again computing the data in the php so this can also be further reduced so what we can say is here instead of doing this flat map using this and using the flat map is the same thing so if i say flat map and here let me say posts like this so this is the higher order message in the collection if you don't know anything about this then you can watch my collection video where i have explained about the higher order messages or hierarchy functions there so it is hiding all those extra boilerplate but it is doing the same thing here all the loops are being performed under the hood so it will take a little bit more memory to compute all the posts from the country yeah so we can get all the post from a country like this now let's see how to get all the post from a country using has many through relationship now for that we have to go in the country model now we want to get all the posts from the country so let us define a method relationship relationship method name as post now here we will return has many through relationship return this has many through relationship and now we can say a country has many post through users class okay now what we can say is a country has many posts through this user model so if we define the relationship like this then we can here directly access the posts if we say like this here now you can see here we are getting the same result here so we don't need to go and find the users and again do a flat map and again find the post so we don't need to do all that kind of thing so we can directly access the post from a country so as you can see here it is returning here database yellow point collection so if i go here and back and if i again refresh here so you can see here it is giving the support collection so there is this difference as well so just keep in mind that we have seen this here in the tinker well now let's see this in the browser so if i go here before going to the browser let me go to the home controller here so whenever we visit the home page it will hit this index method here so here i am getting all the countries here yeah and passing that into the home blade view here if i go in the home so i am looping through each country and i am getting the post from each the countries and i am just showing them here yeah now if i go here in the browser in the home page and if i refresh here now you can see here i am getting all the posts so i am getting this united states so i'm getting this much post and from india i'm getting this much post and from ukraine i am getting this much posts so as always we have seen in my previous video tutorial we are again running into the n plus one query problem yeah you can see here sorry here we are getting all the countries and together to get the posts from each country we are running extra query for each country so what happens is whenever we access this post method here it will run an extra query so to minimize that we can do either lazy or eager loading as before that we have discussed in our previous video now if i go here in the home controller so we can lazy load the relationship like this sorry eager load the relationship like this here so we can get the relationship name here and the relationship name is p-o-s-t-s posts now if i do it here like this so you can see here the number of queries being run is 4. now if i go on here refresh then you can see here the number of queries has been reduced to 2 so we have decreased the n plus 1 query problem similarly the lazy loading we can do it like this so we can do load here like this and we can do posts and this works the same as the eagle loading so the number of queries is two here so one thing to remember that this load method will only be available in the eloquent collection it will not be available in the normal collection okay so just keep in mind that okay now we have seen how to get the relationship and minimize the n plus one problem now let's see how to get all the countries that have at least one post with them so what we can do is here we can run the has method here and we can say here posts like this and if i do it like this and run here so it will get me all these countries list which have at least one post now all the countries have the post here so now if i go here in the database here in the user so you can see here a country you united states country has three users here one two and three and if i go here one two and three users have this six posts now if i remove all these posts here like this here now if i go here in the browser and refresh the page now look here we are not getting united states we are only getting the india country as a post here so here you can see all these countries are repeating here all most of the time so we are only willing to see the list of the countries that have at least one post so all these are repeating because we are again looping through the posts here so let me just remove these posts here yeah and here let me also remove this one now if i come here and if i refresh here so we are only seeing the countries that have the posts and we can also show the post count like this so let me show you quickly here so there is a method called with count here in the query builder and we can give the relationship name here posts like this so if we give the with count and posts like this then we'll get the posts count so let me show you here in the tinker well as well so what we can do is we are getting the country here so let me show you quickly this so we are only getting the country data here yeah so if i do here with count and here if i give the relationship name so if i refresh here so you can see here i am also getting this posts count so we have deleted the posts from the country one so for the country two let's see so there are six posts for the country so it will give the total counts and the key will be posts under underscore count so the posts is the relationship name and it will suffix underscore count there okay now we have done this here now let's in the welcome blade what we can do is we can say here like this and country posts count we can do it like this yeah and if i go here in the browser and i replace here so i'm seeing here six and let us also write the posts here and we can see here india has six posts and ukraine has also six posts and the united states do not have any post and that's why it is not showing here if i remove this like this and if i do here like this then you can see here we are getting the united states and the post as well so now we have seen the hash relationship so we also have doesn't have so here so let me remove this load we don't want this anymore now when we apply this doesn't have method then we will be getting the country that doesn't have any posts okay so for this also we don't need this with count because we are looking at the country that doesn't have any post so we don't need that count as well it because it will always be zero now if i go here and replace the base then i'll only see this united states so now you can see here we are getting only the united states here okay let's say we want to get the countries that have at least five posts with them so you can choose the number either five or hundred but will be only showing the countries that have at least five posts so we in here the both the countries have six posts yeah so let me remove these two and now if we see here for the user for the country is india so the in for the india there are six posts and for the ukraine there are only four posts and when we apply that code we're forgetting only the country that have five posts then we should only see india so if i go here in the code for that what we can say is has posts greater than or equal to five and if we do it like this and come here in the browser and refresh the page here now you can see here we are only getting india so we are not getting the posts count because we remove that yeah so we can say with count here posts and now we'll also be seeing the posts that india has yeah and let's say we want to show the countries that have the post count less than five then we can do it like this and if i go here and you will see here i am getting both the united states and ukraine because they don't have five posts so similarly the there is also doesn't have methods so it will also work in the similar manner okay now we want to get the list of the country that have all the post as active so here if we go here in this so ukraine has so these are the four posts of the ukraine and these are the six posts of india and let's make all these inactive for now yeah okay now we want to get the countries that have the active post only so what we can do here is where has posts and we can pass a method here query and this query builder is related to the post model so query where is underscore active as one so we are saying here we get all the countries whose posts are active yeah so we will not be getting the countries that have the elective posts now if i go here so it means the at least one post should be active now if i go here in the browser and refresh the page so you can see here we are only getting the india yeah and now if i go here and make at least one as active for the ukraine now if i go here and refresh now we are getting ukraine as well okay now let's say we want to get all the countries that have all the post as inactive yeah so we can say where doesn't have where it doesn't have active posts yeah so here we if we see here so both india and you can have the active post at least one active post there yeah so they both will not be visible here so if i go here and refresh the page so we are only getting the united states because united states doesn't have any post and so it doesn't have any inactive post as well so it is showing here so if i make it zero here yeah all the posts are inactive for the ukraine yeah now if i go here and refresh the page so you can see here i'm getting ukraine as well since it doesn't have any post as active all the all of them are inactive so i would like to show you one more thing in the end let's show all the countries by their posts count so if they have the more number of posts we'll show them in the beginning and if they have least number of posts then we'll show them in the end so what we can do is so with count if we apply this with count we'll be getting posts underscore count yeah in a in our query so then what we can do is just say order by posts underscore count and in descending order so we'll be getting all the countries and the post count and we'll be ordering them by the post counts in the descending order so we'll be getting the countries with the highest number of posts on the top and those with the lowest in the end now if we see here in the database so we have zero post for united states yeah so we should see that in the end which we on the top has india and in the second we should see this uk because ukraine has only four posts now here if i refresh so you can see here india has six posts you can has four posts and united states has zero post so it's correct now if you want to order them in ascending order then we let's just remove this descending and now we'll be seeing the united states on the top and then ukraine on the second and india on the last so that's all for has many through relationship i hope you enjoyed this video so if you want to see more videos like this then please don't forget to hit that thumbs up button and also hit subscribe button so thank you for watching have a great day bye
Info
Channel: Laratips
Views: 2,007
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel 8 advanced, laravel advanced, laravel advanced tutorial, laravel 8 advanced tutorial, has many through, has many through relationship, laravel has many through, laravel has many through relationship, laravel tutorial for beginners step by step, laravel tutorial, laratips
Id: Fa_VxmKVo1U
Channel Id: undefined
Length: 20min 2sec (1202 seconds)
Published: Wed Oct 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.