Playing
26. HasOneThrough gets ofMany support

Transcript

00:00
I really love it when changes are made to eloquent relationships and how things are accessed because it pretty much always means there's a much easier and cleaner way to access the relationships that we need and they generally read a lot better. So what's the change that we're talking
00:15
about here? Well it's that we have the ability to access one of many through a has one through relationship. If you are new to Laravel or you haven't worked with more advanced relationships like has one through, this might sound a bit confusing but I'm going to give you a very
00:33
practical example so we can see how this works in practice. So we have a user here who has many projects. Let's just take a look at the database schema here. So we've got our users and sure enough there I am. We have a bunch of projects which
00:47
belong to that user. So I've just got one here at the moment called CoCourse and then we have a deployment table which link up to the project. Now for a standard has many through relationship this is perfect because if I wanted to see
01:05
all of the deployments that I have made regardless of which project they are part of I can use a has many through. Let's take a look at that first and then we'll go ahead and look at the change that's been made here. So let's go over first of all to the web
01:22
root. So I've just fake logged myself in using login using id and I'm going to die dump on auth user and let's say project just to see the projects in the browser here and yeah sure enough you can see I've just got one project here
01:36
which is CoCourse. What we want to do is access all of our deployments regardless of which project they've come from. This is nothing new in Laravel but what I'm going to show you after this is new. Okay so how do we do this? Well let's go ahead and create out a relationship.
01:51
So let's call this deployments of course and this is going to return to us a has many through. You don't have to type in the return here but I'll go ahead and do that. So we're going to say this
02:06
has many through and we what do we want? Well this kind of reads like it should. So we have many deployments through projects and that's all we need to do as long as we've kept the standard within Laravel this should now work. So now when I
02:23
die dump this let's just have a look we've got three deployments which makes sense so we're accessing them via them projects. Even if I had a different project so let's say something else in here that still belongs to me and let's go over to our deployments
02:41
and let's duplicate one of these down and switch that to project two and we'll set the now date here so this is now our latest deployment but for that other project and we go over to the browser we've got four. So if you didn't already know or you needed to brush up on has many through
02:59
that's pretty much how that works. So what we now want to do and this is where this new can be one of many being added to has many through or has one through in this case which we'll kind of dive into. How does this come into play? How does this work?
03:18
Well let's go ahead and say create out a latest deployment and let's just figure out together how we would get this. Okay so let's go and return here this deployment because we're going to sort of leech
03:36
off of the relationship we've already created and maybe let's just say latest so we're going to scope these and order by the created that day. Okay that kind of makes sense so that doesn't give us what we need at the moment but let's go ahead and say latest deployment
03:53
and let's dump that out. Okay yeah so we just get the collection back because we're just sort of leeching off of that already defined relationship and sure enough this is the latest created deployment it's the first one. So what we could do here is then just
04:10
say first that does give us back the latest deployment. The only issue with this approach is that what we're doing here is if we're outputting this in a list we're accessing this relationship over and over again we're probably going to end up with an N
04:25
plus one problem. It's not great because when we work with the latest deployment that just gives us the model back and doesn't give us the relationship back so we can't do anything with that very easily. What we ideally want here is a one relationship.
04:40
Now how do we convert a has many type relationship even if it has many through to a has one relationship? Well we want this to return a has one relationship or has one through relationship we're going to just use the one method.
04:58
Let's just take a look at this and make sure we're dying and dumping just the latest deployment. Let's see what we get and there we go we get exactly the same result. So rather than grabbing the entire collection of latest deployments
05:12
and then going ahead and plucking the first one from that collection which is incredibly inefficient particularly if you have thousands and thousands of deployments or whatever you're working with here we've done that now. So now the change that's been made here
05:29
is latest of many. So we're going to say latest of many on the end of this what that's going to do is it's not only going to pluck the latest one or the only one it's going to do this and it's going to give us the latest
05:45
deployment in that list. So I've got the github repo up here as you can see I'm using the exact example that has been given here so you can go ahead and explore this and apply it to your own projects but that is a really nice change and
06:01
again it just makes it a lot easier when you're defining these relationships with these nice additions rather than have to find a workaround solution or even worse go with more inefficient solution.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!