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!