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.

Episode summary

In this episode, we're kicking things off by setting up a practical example to explore Eloquent's has one of many relationship feature in Laravel. The scenario we're using is a simple forum setup: we've got users, discussions, and posts. Think of each discussion kind of like a thread, and each post as a reply within a discussion.

We start by setting up our database tables and models for users, discussions, and posts. Most of the episode is dedicated to quickly scaffolding out the models and migrations, keeping things intentionally simple so we can focus on the relationships later. Once the migrations are in place, we use Tinker and some manual database seeding to create a few fake users, discussions, and posts. We pay particular attention to the timestamps of the posts so we can later easily determine which was the latest one in each discussion.

From there, we make a very basic UI in Blade to list all the discussions. For now, it just displays the discussion title, but we're prepping things so that, in the next episode, we can show details about the latest post (like who made it and when).

If you're after a fully-featured forum, we mention there's a bigger course available, but here we're just using this setup as a learning sandbox. In the next episode, we'll dive into defining the has one of many Eloquent relationship and use it to show the latest post for each discussion. Stay tuned!

Episode discussion

No comments, yet. Be the first!