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
08. Defining one to many relationships

Episodes

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

Transcript

00:00
Okay, so we're now moving on to probably the most used relationship type within Laravel and that is one-to-many and in this section
00:07
We're going to be using the example of a user having many posts We're going to do this within our routes like we did for one-to-one and then we're going to go ahead and build out a practical app at the end where we're going to have a timeline where a user can post many posts and Hopefully that will give you a really good idea of this very basic, but really important relationship type
00:28
So I've already gone ahead and created a Laravel project I've switched my database credentials around and I have the database open just here. I haven't migrated anything yet Let's do that together If you are following along and we should be good to go
00:41
So the first thing that we're going to do is head over to our web routes We're just going to clear out this welcome view and before we do anything We're going to take a look at defining the relationship now If we had a list of posts for a specific user, we obviously need these in the database
00:57
So that would involve like we did before creating our model so let's go ahead and make out a model here and this is going to be a post model and We're going to create a migration alongside of this with that M flag Let's go ahead and fill this in and you will see that this is very similar to a one-to-one
01:14
So we're going to open up the create post table Migration file in database and migrations and we're going to come down here and fill this out So like we did in the last section with a one-to-one this still had a foreign ID that related to the user But remember the difference between a one-to-one relationship and a one-to-many relationship is really just how this is accessed
01:38
We saw that with a one-to-one relationship We could still have many records, but it's only ever going to give us back the first one You're going to want to use a one-to-many relationship If you always know the user or whatever model you're applying this to is
01:52
Going to have many of these things in our case a user is going to have many posts So we still use a foreign ID here. We can still constrain it at the database level So we know that that user needs to exist. That's always pretty helpful and that creates that foreign key for us and That's pretty much it
02:10
So now we can just stop filling in any of the other data that we would expect to see for a post now for this I'm just going to create out a text column here with the body That's pretty much all we need because from this post we can then get the user which is what we're going to be covering in The next section. So now that we've defined this model out
02:28
We're going to go ahead and migrate and that's going to create that over in the database here With that user ID and of course, we don't have any users at the moment We're going to go through the same process of just generating out a fake user here So let's go over to our tinker session again
02:43
we're going to use our user model here use the factory for that user which we looked at in the last section and Create that out. There we go. So now we have a User in here We're now going to head over to the user model not that we need a user to do this and we're actually going to define
03:02
The relationship out here. So let's go all the way down to the bottom I tend to put all the relationships at the bottom of a model and we're going to go ahead and create out a Posts method inside of here. Now, let's go ahead and define out the very basic relationship type here This is going to be has many and then we just go ahead and give the name
03:23
or the fully qualified namespace to the model that it has many of and That is pretty much it. That's all we need to do at the most basic level There are some things that we can add to this which we're going to be looking at later. But for now, that's it So we're gonna go over to our web routes here
03:39
We're gonna fetch this user out and we're just gonna see what's returned when we actually access this relationship first of all So let's go ahead and find out the use that we've created that's by ID 1 and just make sure that we pull the namespace in
03:54
for the user just at the top here if your editor doesn't do that for you and then we're gonna just die dump on the Posts relationship we access this in exactly the same way unless we're inserting So if we're reading this data, we don't use the method call We use this like a property and Laravel will fetch these this information for you or eloquent
04:16
We'll fetch this information for you and it's head over and see what we get So I'm going to give this a refresh now when we looked at one-to-one and we access an address We got back a model this time We've got back a collection because we have multiple potential multiple items inside of here
04:34
We could still have one but it's still going to be returned to us as a Laravel collection Now Laravel collections are a whole other topic in themselves These have some useful methods on that you can use to change around the items that exist within them but the majority of the time you're just going to be
04:51
Iterating through a collection a collection allows you to iterate over it really nicely and we will be looking at that But that's pretty much what we get back. So not too different from a one-to-one relationship here We head back over to the user model We define this using a has many and we access it in exactly the same way
05:07
The only difference is rather than we getting back a post model, which wouldn't make any sense That would be a one-to-one relationship. We get back a collection of Models, which we're going to see shortly and this will contain many post models. So there we go That is how we define out a one-to-many relationship
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.