06. Scoping to get the most recent address


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


one-to-one relationships within Eloquent can actually be a little bit confusing because if we run the create route that we've created twice, this is actually going to create two address records. So really this catches a lot of people out in the sense that one-to-one
doesn't really just create one record, it allows you to access one record from a user. Let's explore that in a little bit more detail, but the first thing we're going to do is just demonstrate out the fact that this does create more than one record. So we don't have anything in the database at the moment, I'm going to head over to create and that's created 38 code
road. I'm going to go ahead and just change this around a little bit and I'm going to say one code lane and we're going to head over and run create again. There we go, we have two records in the database now, so technically this user has two addresses. So like I said the one-to-one relationship does not stop you from creating more than one thing for a particular user or any other
model that you're dealing with, the relationship works by just returning one thing. So this is why in the majority of cases you do not need to use a one-to-one relationship. It's important to learn about it, understand how it works, but most of the time you will use a one-to-many relationship which we will be covering and with a one-to-many relationship what you can do is still access the
latest record and there's in fact a modifier, a relationship modifier that allows you to do that very easily which we'll be covering. But let's look at how we, if we did want to use a one-to-one relationship, how we access the latest address because at the moment we've created two addresses but the user has one address. We kind of want to return the latest relationship. Now we can do this
with scopes. We can do this either at the level where we are accessing the addresses or we can do this at the relationship level itself. So let's take a look at how we would do this. So at the moment over on our home page we're grabbing the user, we're passing that through to our address view and over in the address view we are checking if the user has an address. If they do we're going
ahead and grabbing line one. So what we're going to do instead is we're going to say user or we're going to define a variable here. We're going to say address, user, address and we're going to say latest. Now notice that I have used the method here rather than the property which gives us back a Laravel collection that's just a collection of the models. So we're going to say that we want
to scope this by the latest. This is effectively a hidden order by which we'll look at the alternative in a minute and let's take a look at what we get. So I'm going to go ahead and die dump on this new address variable that we've created and let's head over to the home page and give this a refresh. So at the moment we don't actually have a record we just have a builder which is building up this
relationship. So what we want to do on here is save first. So now if we give this a refresh, notice that we have one code lane. So that will grab the latest record from the multiple addresses that we have. Now latest is just syntactic sugar over the order by. So we can order this by created at in ascending or descending order. So if we were to give this a refresh we get 38 code road.
If we were to do it in descending order we get of course one code lane. So this is just a query builder building up how we want the SQL query to be executed before it's returned to us. But order by is hidden behind within Laravel under latest. You can use either one it does not matter but latest tends to be a little bit more readable. Okay so this is a little bit annoying. We have a
one-to-one relationship we have two addresses in the database but we really only want the latest one. How do we get around this? Doing this probably isn't recommended unless you really need to. What we can do is instead over on the user model we can say well for this one-to-one relationship just here we always want to get back the latest address. It just makes sense for the logic of our
app. So what we can actually do is use the latest scope within the address relationship method that we have created. So now that we have done that let's head over to our home page and you can see sure enough we get one code lane. That means that over in our web route if we were to create another address let's say 100 code lane and we head over and create this and we head back to the home page
sure enough it will always show us the latest address that has been created. So this might be something that you want to do. A user adds a new address it adds it on to a kind of history of addresses but we always get back the first address and the difference between a has one and a has many relationship is that inside of your templates you're always accessing this like the user
only has one address. In terms of your application the user only ever has one address and it's a lot more convenient to access if you know that you only ever want one thing returned. If you are very new to Laravel once we get over to one-to-many relationships you'll very quickly start to see the difference between one-to-one and one-to-many and when these can be useful.
33 episodes4 hrs 18 mins


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!


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