This episode is for members only

Sign up to access "Build a Forum with Inertia and Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
11. Setting up discussion posts

Episodes

0%
Your progress
  • Total: 6h 54m
  • Played: 0m
  • Remaining: 6h 54m
Join or sign in to track your progress
01. Introduction and demo
6m 59s
0%
02. Getting set up
10m 36s
0%
03. Modifying registration for usernames
7m 15s
0%
04. Figuring out the forum layout
5m 57s
0%
05. Creating and listing topics
9m 15s
0%
06. Basic discussion listing
13m 33s
0%
07. Pinning discussions
4m 1s
0%
08. Tackling pagination in Inertia
8m 23s
0%
09. Customising pagination text in Laravel
52s
0%
10. Showing a discussion
6m 4s
0%
11. Setting up discussion posts
5m 53s
0%
12. Listing through discussion posts
5m 28s
0%
13. Adding more data to posts
8m 24s
0%
14. Adding pagination to posts
1m 35s
0%
15. Adding a post preview to discussions
4m 52s
0%
16. Adding the last reply to discussions
5m 54s
0%
17. Outputting discussion participants
8m 4s
0%
18. Limiting participants in the UI
5m 56s
0%
19. Ordering discussions by last post
4m 30s
0%
20. Handling deleted users
2m 31s
0%
21. Counting replies
8m 13s
0%
22. Building our first filter
8m 31s
0%
23. Highlighting current filters, and merging with pagination
5m 37s
0%
24. Adding auth specific filters
6m 40s
0%
25. Adding the topic filter
8m 18s
0%
26. Scaffolding the new discussion form
13m 29s
0%
27. Toggling the create discussion form
9m 2s
0%
28. Keeping form state
4m 59s
0%
29. Storing a new discussion
11m 29s
0%
30. Discussion validation and authorization
5m 1s
0%
31. Generating markdown for posts
8m 37s
0%
32. Toggling the markdown preview
7m 43s
0%
33. Fetching and displaying markdown
8m 6s
0%
34. Adding a markdown shortcut toolbar
5m 53s
0%
35. Dealing with SVG icons
7m 46s
0%
36. Creating the reply form
7m 48s
0%
37. Basic Inertia permission checking
6m 26s
0%
38. Creating replies to discussions
5m 37s
0%
39. Jumping to posts
11m 40s
0%
40. Automatically scrolling to posts
6m 18s
0%
41. Toggling post editing
7m 32s
0%
42. Editing posts
3m 36s
0%
43. Deleting posts
4m 21s
0%
44. Deleting discussions
6m 7s
0%
45. Setting up for best answers
7m 29s
0%
46. Toggling the best discussion answer
12m 14s
0%
47. Solved and unsolved filters
2m 23s
0%
48. Indexing discussions for search
8m 6s
0%
49. Searching discussions
12m 35s
0%
50. Debouncing search
2m 47s
0%
51. Adding mentionable functionality to forms
6m 32s
0%
52. Indexing users for mentioning
9m 9s
0%
53. Hooking up users for mentions
10m 10s
0%
54. Detecting and storing mentioned users
9m 54s
0%
55. Adding the mentioned filter
2m 26s
0%
56. Adding mentions to the markdown toolbar
1m 1s
0%
57. Adding mentions to the reply form
7m 21s
0%
58. Fixing up some unauthenticated state
1m 1s
0%
59. Fixing up post scrolling
1m 48s
0%
60. Reviewing SSR (Server-side rendering)
8m 20s
0%
61. Preventing parent posts from being deleted
2m 31s
0%
62. Improving solution marking
4m 9s
0%

Transcript

00:00
So here's how posts are going to work within our discussions. When we create a new discussion like we already have manually in the database,
00:07
a post will be created separately alongside to that and attached. So our discussions themselves over in the database won't have the first post within them. That makes it really easy to deal with posts individually rather than having to work out whether they're the first post of a topic.
00:24
We can still work out whether they are the original and first post, but we can do that within Laravel Relationships. So with that said, we're going to get started on setting up our posts model and all of the relationships that we need.
00:38
So let's go ahead and start by making out a post model here, of course, with a migration. And let's open up our create post table and just start filling this in. So once again, this is very similar to our discussions. We're going to have a foreign ID for the user, but this is going to be nullable.
00:56
So let's get rid of this and we'll put nullable in there. We'll constrain this like we did before and on the deletion of this, we'll go ahead and set this to null. That just means that the user's posts will stick around even if they have deleted their account.
01:11
So the next thing is, of course, really important, a foreign ID for the discussion that this post belongs to. So for this, we don't need to make this nullable. We can just constrain this and we can say on delete cascade.
01:24
That just means that if a discussion is deleted at the database level, it will delete all of the posts within that discussion as well. So the next one is a little bit more difficult to understand and we're kind of getting ahead of ourselves.
01:38
This is going to be the parent ID. Now, let's go ahead and just constrain this back to the posts table that we're creating. And let's set that to nullable as well. And I'm just going to move this because sometimes the ordering of these
01:53
doesn't quite work out. OK, so why are we including a parent ID? Well, let's think about this. We have a discussion maybe with an ID or a post with maybe an ID of one.
02:05
Post two rolls in, which is a reply to the overall discussion. But we're actually going to set that as a reply to post one. The reason we're doing this now is later on if we wanted to add nested replies. So if we wanted to reply directly to a post,
02:21
we have a parent ID in there, which means we can then nest them replies. Now, the other reason we're doing this is the first post that we create is not going to have a parent ID because it's just attached to a discussion. But any subsequent posts are going to be replies to that original post that was created.
02:41
This just makes it really easy to work out what the original post was on the forum, but also open this up so we can add nested replies later if we need to. So we'll cover that later, but let's fill in the rest of these. We're going to set the text here or the body here to a text type.
02:57
And for now, that's pretty much all we need. So we can go ahead and migrate this and we're done. So we're going to now head over to the post model and just fill in a couple of things that we need to like the fillable fields.
03:11
So let's add our fillable in here. And for now, at least that's just going to be the body. So we're going to head straight over to our discussion model and let's add some relationships in here.
03:21
So a discussion is, of course, going to have many posts so we can access that. So let's say this has many posts, again, really simple relationships. But we're also going to create out a individual post relationship. And you can change the name of this if it's not quite clear enough.
03:42
This is going to be the first post of the discussion. So this will have one post. And remember, we just spoke about the condition around that. That's going to be where the parent ID is null.
03:53
So that's just attached to the discussion. It's not any subsequent replies that we've created. So we can use these two relationships to get all of the posts, but also work out what the first post is as well if we need to.
04:06
Now, over on the post model itself, we want to relate this back to a discussion. So let's create out a discussion relationship. And again, the relationships here that we're dealing with are actually pretty straightforward.
04:18
This belongs to a discussion. Now, a post also belongs to a user. So we can create out a user relationship as well here. So we can work out who posted this.
04:28
And this belongs to a user. And while we're here, let's hop over to the discussion model and just check that we included the user relationship here. And it doesn't look like we did.
04:40
So let's go ahead and grab the topic relationship and set that to user. So we now can find out who posted a discussion. We can get the posts for a discussion, and we can also work out who posted each of these posts.
04:57
To finish up, let's create a couple of fake posts in here just to again demonstrate the parent ID and how everything fits together. So I'm going to go ahead and set this as me. The discussion ID is going to be the need help with Laravel discussion.
05:10
And the parent ID is going to be null. So first post, and let's fill in the created at and updated at. Let's save this out. And then let's create a reply to this overall discussion.
05:23
What that's going to do is have a second post where the parent ID is going to be this one. So then we can find out what's being replied to the original discussion. And the discussion ID is going to be the same.
05:35
And we'll keep the user ID the same. We'll just switch over the created and updated at dates just so that appears below that. There we go.
05:43
Let's save that out. And we have two posts attached to that discussion. So our job now is to iterate through these posts and show them on our discussion page.
62 episodes6 hrs 54 mins

Overview

Ready to build a forum with Inertia and Laravel?

Why a forum? A forum touches a whole load of concepts that you'll use throughout your development career – particularly on the client-side, where we'll be doing most of the heavy lifting.

So, let's build a clean, modern forum with features like markdown support, code highlighting, advanced filtering, user mentions, full-text search, the ability to mark best answers, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!