Playing
02. Comment model basics

Transcript

00:00
So at the heart of all of this is of course a comment model, so we'll get started with that now
00:05
We might take a look at child replies to comments as well Depending on how far we go or we might cover that in a separate episode. So let's start with the absolute basics We're just going to keep this really simple now, of course, we have flexibility to add to it So let's go ahead and make a model called comment. We'll create a migration and a factory
00:25
alongside of this as well So let's go over to the create comments table Migration now, obviously this is going to be attached to a user So the first thing that I'm going to add in is a foreign ID to our user
00:40
We'll just call that user ID and we'll go ahead and make this constrained. So it always has to be attached to the user Okay. So next up is just the basic metadata about a comment, which really to be honest is just a body So for this we're going to say table and we'll use the text type to say body and I think that's pretty much it to be honest. There's nothing really else. There's no titles for comments. It's literally just a comment
01:07
We'll have the timestamp here So we don't need to do much else for that But we will be looking at whether this has a parent ID because we're going to keep comment replies in the same table and have them self-reference each other
01:22
We'll take a look at that later But let's just go ahead and say phpArtisanMigrate and just get that in the database ready to go Okay. So next up let's create the relationship for a user. Now, we're not really going to write a test for this I don't tend to write unit tests or even feature tests to link up different relationships
01:42
You can do if you want to but I Generally don't think it much makes much sense because the tests that we are going to be writing will verify this relationship exists anyway, so we're going to have a comment relationship in here and we're going to say this has many and Comments and class that's it. That's pretty much what we need to do for that
02:02
Let's hop over to our comment model and just make sure that everything looks good here Remember in the last episode we set model on guard so we don't need to add any fillable columns in here We can link this back to a user at this point. So let's go ahead and say that this belongs to a user belongs to and
02:19
passing that user there and we should be good now what I want to do is make sure that I can generate a bunch of Comments for testing either within my tests or as a seeder for when we see our application So under the comment factory, this is where I want to do this So with this I generally don't fill in a user in here. I don't I don't fill a user ID in
02:40
I don't set up a specific relationship. I'll just do the sort of metadata So for this we're going to use the fake helper and let's say sentence and let's say 20 words We can always adjust that. So now what we should be able to do is We could either do this obviously within our test, which is where this is going to be most useful
02:58
But we can do this directly within a tinker session or within a seeder now so we can say app models comment we can say factory and We can say create now that will use the body that we've created here But a user will not be associated with this
03:17
So if we try and do this, it's just going to break now What we can do is we can do multiple comments so we can say factory 20 or we can say times 20 there's a couple of different ways that you could do that more recently. I've just been passing this directly into factory So for this we say for and it's for a user so in here what we now need to do is say user
03:39
Factory, so what this is going to do for every single comment that we create here It's gonna generate our user and assign it to that particular user So if I run that now what we'll end up with is if we head over to the database and go over to comment we've now got a bunch of comments in here and they are associated with a particular user and
04:01
Yeah, that's just done it for one user which is absolutely fine. It's really all we need and Yeah, we're just gonna be using this for testing. So for now, I think that's absolutely fine Okay So now that we've got this comment in place
04:12
I think we need to think a little bit more deeply about how we are going to attach comments to things By that I mean we're probably gonna end up with and we will in this course with a couple of routes like this So we're gonna have something like articles
04:29
Slash article Now that's gonna hook this up to a controller Which is going to take some sort of article slug ID and it's going to render that out and then have comments underneath that But then we also might have so for example directly on co-course. We might also have a episode so we might have episodes
04:49
Episode and that should also have comments as well. So we need to think about the polymorphic relationship here That's going to allow us to attach comments to any model type in our application both now and into the future So now that we've got the basic comment model set up Particularly if you are new to Laravel, let's now go over and sort of start to dive into polymorphic relationships here

Episode summary

In this episode, we dive into the basics of setting up a comment model in Laravel. We kick things off by creating the Comment model, along with the necessary database migration and factory. Our first focus is just the essentials: every comment needs to belong to a user and have a body, so we add those fields and set up the corresponding relationships.

We talk a bit about comment replies (like parent-child relationships) and note that this will probably be covered in more detail in another episode. For now, we’re just scaffolding out the basics so we can iterate on it later.

Once the migration is set, we run it to make sure the table exists, and then jump into setting up Eloquent relationships. We link up users and comments, so every user has many comments, and every comment belongs to a user. We also touch on why we don’t bother writing separate tests for these relationships, since the main application tests will cover them anyway.

To make things easier to test, we tweak the comment factory using Laravel’s fake data helpers, so we can generate a bunch of comments quickly. We go through how this works in tests, tinker, or seeders, and show how to associate generated comments with users easily.

Towards the end, we start thinking about how comments need to attach not just to users, but maybe to different types of content (like articles and episodes). That means we’ll need a polymorphic relationship, which we’ll start tackling in the next video. For now, we’ve got our foundation set up and we’re ready to expand on it!

Episode discussion

No comments, yet. Be the first!