Laravel Gems - Where Relation 💞

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello level friends I got another level champ for you today this one was already introduced in level 8 today we're talking about the ver relation eloquent method that helps you checking for a relationship existence follow me in LEL we often need to retrieve items depending on a relationship so in this project for example we have Airlines and we only want to get the airlines which also have flights so what I can do is I can use the has method here and I can just provide the relationship which is flights and then I'm using the plaque method in order just to get the name of our Airlines and if we run this here and Tinker well you can see I get four Airlines back which all have flights which is pretty cool because if they wouldn't have flights it would be a pretty bad Airline all right but here we can also do more we can also say we only want Airlines where they have at least 100 flights and you can see only Austrian Airlines has 100 flights but here we're limited to just the count of flights but not like columns on the flight model itself so what we can use here instead is also the wear has method the first argument again is the relationship but the second one is now a call back and here we retrieve the Builder and here we can run wear conditions on flights so we only want to get flights where is delayed which is a column on the flights field is true so we only want to get delayed flights and again we want to get only the airlines with the names and you can see we have three Airlines which have delayed flights but there is a nicer way to write this let me show you but first we are checking out what the SQL statement looks like for this query we can tarle this in here in Tinker wall and yeah we're selecting the name from Airlines where exist is the method we're using here and now we're checking all the flights with our wear conditions here okay so now the method that I want to show you is called relation and the first argument is still the same relation flights but now the second one is directly our column and the third is the value so we can get rid of the rest directly use plaque method and if I run this you can see the query looks the same and the result also looks good but this is now a much nicer and cleaner way to use this with the wear relation method but we're also not limited to just making equal comparisons here to True let's check another column on our flights table which is called expected start at so when is the flight expected to start and we want this to be smaller than in the next 30 days let's run this and yeah we get a result back so this looks good and yeah here what we're checking now is give me all the airlines that have flights with expected start at date in the next 30 days so this is now a pretty cool way that you can use the r relation method and it's even more powerful if you want to use different comparison like the one we did here if you haven't used this method before please give it a try right now and I'm pretty sure you will fall in love with it as well have fun and see you the next time
Info
Channel: Laravel
Views: 5,335
Rating: undefined out of 5
Keywords:
Id: uYwJ_t1RmkE
Channel Id: undefined
Length: 3min 18sec (198 seconds)
Published: Thu Feb 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.