This episode is for members only

Sign up to access "Eloquent Relationships By Example" right now.

Get started
Already a member? Sign in to continue
Playing
13. Adding modifiers directly to the relationship

Episodes

0%
Your progress
  • Total: 4h 18m
  • Played: 0m
  • Remaining: 4h 18m
Join or sign in to track your progress

Transcript

00:00
Something that you might need to do when you're defining out relationships themselves is add in the scopes or the additional query elements to the relationship itself. Now, let's just take a look at how this works. It's not always going to be the case, but it's useful to know.
00:18
So we're going to return back to our post list. What I've done is in the database created first, second and third post. We know that these are ordered by latest at the top. So we have our third post at the top, and we did this using the latest scope, or we could use that order by defining in the column and then the direction.
00:37
So either way is going to work. But what happens if you always wanted posts to be in this order and you were outposting these in multiple different areas of your application? Well, you'd probably want to add this within the actual relationship itself. I wouldn't necessarily recommend doing this because you might need at some point to not show these posts in the latest order.
01:03
So it's not always a good idea, but there are times when this is needed. So I'm going to return this back to latest. I'm actually going to remove this from here entirely, just back to the point where we say posts and get. Now, that's just going to order that in the natural order that we get back from the database, which is the first post first, second and third.
01:22
Now, what we're going to do is head over to the user model over to our post relationship. Now, on here, what we can actually do is still use these methods because it's exactly the same thing that we are getting back when we applied this over here. So accessing that method and here is just the method. So we can apply these scopes or ordering or any kind of constraint directly to the relationship.
01:50
Like I've said, it's not always the best idea because we need to access posts in a different order later on. You would have to go ahead and undo that and then redo it where you need it. But if we would say that posts always needed to be latest, then this is a great way to do it. So if we just come over here now and give this a refresh, you can see that that works in exactly the same way.
02:11
The benefit of doing this, the whole reason that this is potentially a good idea is because now what you can do is get rid of this entirely, this line entirely, because you don't need to access the relationship within your controller or your route to then go ahead and scope this. So I can get rid of this completely and we can bring this back to what we had before user and posts accessing this as a property to get back the collection. Now, by the time this hits the database and returns to us, the scope's already been applied at the actual relationship level.
02:44
So we just get back this in the same order that we've seen before. This can be really useful, particularly when you're working with where causes and you need to exclude certain things always. But this is something that you can do. So any builder method that you have access to that we've previously seen, you can also apply at the relationship level if you need to.
03:07
A good way to think about this, not necessarily for this example, but if you, for example, wanted the standard posts relationship, but then you wanted another relationship which also returned posts, but always in latest, you could do that. Just take a look at a really quick example of this before we move on. So I'm going to go ahead and get rid of the latest scope here and I'm just going to bring this back to user posts in the natural order that we find it.
03:32
But let's say that we wanted to find out a new relationship. There's nothing stopping you from doing this. We might want to create a latest posts relationship type. Now, this is perfectly valid.
03:44
We could still do this. We don't need to name this posts. So we can still access this like this and it will just work. The difference is now that we've created a kind of duplicate method here that also defines this relationship.
03:56
We could explicitly use the latest scope in here. So we've got posts for when we need this in any order and we don't really care, maybe for when we're inserting or a latest post, which will allow us to conveniently access the latest list of posts. And you can see sure enough, now that we've switched that over, we get exactly what we do expect.
04:16
Now, what you could do even to take it even further here is you could access the posts relationship that you have previously defined already and just reuse that from the latest post relationship. That's going to work again in exactly the same way. So really, this kind of stuff is helpful when you have specific methods that need a certain relationship type, but you need a very specific constraint on here or ordering. You can apply these here just so you have a really convenient way to access these directly like this without having to do this inside of your root or controller.

Episode summary

In this episode, we look at how you can add modifiers—like scopes or constraints—directly to Eloquent relationships in your model. We kick things off by talking about a scenario where we want all posts to be shown in the 'latest first' order throughout the app, and how manually adding latest() or orderBy everywhere is repetitive. Instead, you can attach these sorts of modifiers right to the relationship definition in your model.

While this is pretty neat and can help dry up your code, I point out a few things to watch out for. For example, if you ever need posts in another order later, having the scope right on the relationship could get in your way and need to be undone. So, it's handy for consistency, but you have to be sure it's what you want across the board.

Next, I demo how to use these modifiers directly in the relationship method, and show the practical benefit: you don't have to repeat sorting logic in your controllers or routes—just call $user->posts and you're set. We then take it a step further by introducing custom relationships. For example, you could have a standard posts relationship, and another like latestPosts that always returns posts in descending order. This way, you can reuse the base relationship or apply constraints as needed, keeping things nice and clean.

Overall, this episode is all about making your relationships more flexible and expressive, saving you from duplicating logic, and making your code easier to read and maintain!

Episode discussion

No comments, yet. Be the first!