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.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

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

Comments

No comments, yet. Be the first to leave a comment.