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
31. Setting up a practical example

Episodes

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

Transcript

00:00
The has one of many relationship type in Eloquent allows you to access the latest, oldest or some kind of aggregate record from a has many type relationship.
00:13
So the example that we're going to be looking at in this section is going to be like a forum that has discussions and the discussions have hosts. And with this, we can actually come up with some pretty powerful functionality that makes things super quick. So we're going to go ahead and set up a really simple example in this episode, and then we'll get on to looking at all of the elements of this relationship type. So as usual, let's go ahead and get everything set up.
00:39
We'll go ahead and make sure that we migrate the default migrations inside of here and then we'll start to create out some models. So I'm going to go ahead and make out a discussion model and we'll go ahead and create a migration alongside of that. Let's go over to our create discussions table and again, we'll keep this really, really simple. Let's go ahead and create out a foreign ID for the user ID and go ahead and constrain that.
01:06
And we'll just add some really simple details to this. So let's just give this a title. And to be honest, we will just keep it at that for now. We will be adding something a little bit later.
01:18
OK, so we're going to go ahead and say PHP artisan migrate for this, and now we need some posts that belong to discussion. So a discussion will have many posts. That's the basic relationship type we're setting up here. So let's make out a post model with a migration and we'll go ahead and create out a posts table in here.
01:39
So this is going to have a foreign ID for a user ID. So we're going to say who posted this and we need to know which discussion it belongs to. So discussion ID. And for this, we'll again just keep things really simple.
01:54
We'll just add some text in here with a body. But again, we'll be adding some columns later when we demonstrate out some of the other things that we can do. OK, so now that we've got this set up, let's just create some really basic data here. We'll do the discussions and posts manually.
02:10
So we'll go ahead and run PHP artisan tinker to generate out a fake user. So factory and create. We can just use one user for now and we'll head over to our database and create out a few discussions. So let's do these all for the same user.
02:27
It doesn't really matter. So let's say discussion one. And just fill in this and we'll do we'll save this out so we can change the dates around as well. So discussion two.
02:42
Refill in these dates and we'll duplicate this down. And say discussion three. In fact, we'll just keep two because we're going to be creating some. Posts in here as well.
02:54
So let's go ahead and create our post. OK, so let's go and create a post for here for discussion one. And we will create quite a few out here just so we can get the idea of what's happening. So we're going to say one, two.
03:11
And three, but really importantly, what we're working with here is the latest post from a discussion or that's what we're going to be doing. So I'm going to go ahead and modify each of these timestamps so they get incremented, much like we would find in a real application. Obviously, we wouldn't have all of these posts being created in one go. And finally, I'll just go ahead and create one more.
03:33
Post, but for this second discussion and we'll just say one. And we should be good. OK, so let's go ahead and create our really basic output of this data over on the homepage and we'll see what we need to do. So we'll say discussions and index and we'll go ahead and pass these discussions down.
03:55
So reference the discussion model, we'll grab the latest discussions at the top and we will go ahead and grab them. OK, so let's go and create this. Template out, so discussions and index dot blade dot PHP. OK, so for each of our discussions again, seen this before, let's go and output these discussions in a list.
04:21
And we'll have a title in here. So that will be the discussion title. And to be honest, that's pretty much all we need just for now. The goal of this is going to be to show information about the latest post.
04:38
So we want to say who the latest post was created by, when it was created, any of that kind of information. And then we'll look at some other examples. So this is the really basic example. We do have a full forum course to build a forum on CoCourse.
04:54
So you can go ahead and follow that afterwards. And these relationship types will make a lot more sense. But for now, that is pretty much what we're doing. So let's head over to the next episode and look at how we can define and use has one of many relationship to get the latest post and show that information on this forum homepage for each of our discussions.
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.