Defining User-Post Relationship 0:00So far, our resources are just living on their own. So our post resource knows nothing about the user, and the user knows nothing about the posts. But let's change this. So what I want to do is, I want the posts to belong to the user, and one user has many posts. So a classic relationship. To do this, I already added the user ID column to our posts table, but we still need to add the relationship. So let's go to our posts model first.the relationship. So let's go to our posts model first. So in here, we just define that we have a new user function. And here we return this belongs to, and then the user class. And the inverse relationship, if we go to our user, then we can say public function posts, and a user has many posts. Okay, now we have setup that our models know about their relationships, but Nova doesn't know about them. So just as we can add fields for text, datetime, boolean, select fields, etc, we can basically do the same thing for our relationships. Adding BelongsTo Field 1:20So just as we can add fields for text, datetime, boolean, select fields, etc, we can basically do the same thing for our relationships. So our post has a belongs to field for the user. And let's import that. Now what does this look like if we go to Nova? If we go to the posts and refresh, we now see that there's a new column user, which is empty because we have not yet stored the user anywhere. And if we added an existing post, we can now select from users. So now we have a drop down. And here we have the user name.So now we have a drop down. And here we have the user name. And this is what I said when you'll take a look at the resource. So let's take the Nova user resource. The title is the name. So this is the value that you can see in the drop down. So for example, if we change this to email, go back here and refresh, then we see the email addresses here. Let's change this back to name. Okay.Let's change this back to name. Okay. So let's just define that this post is from this user and update. Now on the detail page, we have the user, and it's now a link that points to the detail page of this user resource. That's cool. And basically the same if we go to the post listing, we can also see that this is the user for this specific post. Now let's define that this second post also belongs to the user and add the inverse relationship. So if we go to users and take a look at this specific user, we don't see that this user Adding HasMany Field 3:14Now let's define that this second post also belongs to the user and add the inverse relationship. So if we go to users and take a look at this specific user, we don't see that this user has posts. That's because the Nova user resource still doesn't know about it. So what we do is we go to our Nova user resource, and in these fields, we basically just add a new field similar to the belongs to it's has many make posts and import this. So if we now go back to the listing, nothing has changed. But if we take a look at the detail view, we got an error. Okay. I know why.Okay. I know why. So our user posts. Okay. We need to return this relationship, of course, refresh. All right. Now we see that this user has two posts. So these two posts that we have, we can now see in here all these fields that we have defined for our post resource. And we can also create a new post, which will automatically choose this user for the user Introducing Tags Relationship 4:35defined for our post resource. And we can also create a new post, which will automatically choose this user for the user relationship. So in here, my third posts, somebody I created. And now our user has three posts. And you can do the same searching and sorting and filtering as you can do within your post resource inside of the detail view. Okay, let's try the same thing for more complex relationship. So our posts should also be able to have tags. And a tag has a belongs to many relationship, because the tag can be reused for multipleSo our posts should also be able to have tags. And a tag has a belongs to many relationship, because the tag can be reused for multiple posts. So I already created the tag and the model. But the resource is still missing. So let's add this PHP artisan Nova resource tag. Here it is, it points to our tag model, and the title will be tag, searchable will be the tag. And it just has a text field, which is called tag. Simple as that.And it just has a text field, which is called tag. Simple as that. All right. So if we go here, we now have a new tag resource, where we can just create a new tag, import this field class, and then just create it. So one tag might be Laravel. Another tag might be Nova. Okay, so now we have two tags. So let's add the belongs to many relationship to our posts. First, let's start by adding the relationship to our model. Setting Up Many-to-Many 6:44So let's add the belongs to many relationship to our posts. First, let's start by adding the relationship to our model. So this is our post model. And in here, we will add a new method, posts, tags, and return this belongs to many. Tags, and it's basically the same for our tag. So if we go to the tag model, we will have a function posts, where we define that this belongs to many posts. Okay, now for the Nova fields. It's pretty much the same thing as for the belongs to relationship. So instead of saying belongs to user, we have belongs to many tags, and let's save the file.It's pretty much the same thing as for the belongs to relationship. So instead of saying belongs to user, we have belongs to many tags, and let's save the file. And if we now go to the detail page of one of these posts, we now have all the post details. And underneath that we have this belongs to many panel. So now we see that we have tags, well, there is currently no tag attached to this post. But we can now select attach tag. And we just have a drop down of these tags that are available. So let's say this has Laravel and attach it. And then, as you can see, we now have for this specific post, we have one tag attached to it, which is Laravel.And then, as you can see, we now have for this specific post, we have one tag attached to it, which is Laravel. And to basically see the inverse relationship, all we have to do is we go to our tag resource. And in here, we add belongs to many make posts. Now if we go to our Nova tag, we don't see it attached to any post. And if we go to our Laravel tag, we can see that this tag is being used in these posts. And just like you can attach tags to posts, we can now do the inverse on this page. So we can click attach post, and then just have a drop down of the available posts and attach them to this specific tag. So this is how you can define different relationship methods and relations on your Nova resources. Reviewing Relationship Options 9:25attach them to this specific tag. So this is how you can define different relationship methods and relations on your Nova resources. Of course, if you go to the documentation, under fields, relationships, you can see all the available relationships that you also use inside of Laravel are available here. So you can use them in Nova as well.