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
38. Creating replies to discussions

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
Now that we are done with our reply form, let's hook this up so we actually store a post on our discussions. Now we're going to start over by creating out a controller for this. So let's go ahead and create a post store controller here. And we'll go ahead and register the route for this first of all.
00:20
And we know that we need to be authenticated for this, so let's just go ahead and grab the structure of this. And we're going to go into discussions, choose the discussion that we want to actually post for, or post a post for. And then we'll go into this post relationship, keeping this nice and restful. So let's change the controller over to post store controller.
00:40
And then we'll just call this post.store within the context of a discussion. So over in our post store controller, let's just fill in the invoke magic method here. Right off the bat, we're going to go ahead and create out a form request for this. So let's go ahead and generate a request here.
00:58
And we're going to call this post store request. We know that we have within our discussion policy a reply permission or a reply authorization action, which we can use within here. So let's go ahead and fill in the post store request here. And we know that this is in the context of a discussion, so we can use this to authorize.
01:23
Let's do that first of all, since we've already got this set up. So let's say auth user can, and we're going to say reply to. And because we are within a request here, we can just go ahead and pass the discussion directly in from this request. That should authorize that for us.
01:41
Now, in terms of the rules, we just have a body here. So we're just going to go ahead and say this is required. We didn't have any rules for this when we created a discussion initially. So let's just leave that as it is.
01:52
We can always add to that later if we need to. OK, so how do we create out a post? Well, we can just do that really, really simply with the post model and making it, so not persisting it to the database just yet. And we can use the validated data from the form request, which will just give us back the body here and insert it.
02:12
So it's just a shorthand way of doing that with only the data that's been validated. The next thing is we want to associate this with a user. So we already have the relationship set up in there. So let's do that now. Post user associate.
02:27
And we're going to associate that with the currently authenticated user, which we can grab from the request. Next up, we want to associate this with a discussion, which we know we get through into here with root model binding. So let's go ahead and pass that discussion in. And we now need to speak about the parent.
02:44
So we know we've got this parent ID. If we look over in our post model at the moment, we don't have a parent method which we can easily do this with. So let's go ahead and create one out now. Again, that's going to belong to a post.
02:58
And because we are technically creating a relationship within the same table, we're just going to go ahead and give the parent ID as the column because Laravel won't choose that by default. So now what we can do is associate the parent here with the first discussion post. And we already have that post relationship set up.
03:20
Lastly, we're just going to go ahead and save this post out and then we're going to go and redirect back. We're going to modify this a little bit later because we want to jump directly to the post. And actually, let's fill that out now. So let's go ahead and set a redirect to a route here.
03:37
And let's do that to discussions show and then pass in the discussion just so we know that we're going back to the right page. Remember, we could also be on a completely different page while we're replying because we can browse the forum as we're replying to this. OK, so now that we've got our controller done, we've got the route for this. Let's just start to hook this up over in our reply form.
04:00
This is our create post form. And we know that we already have an action here, create post similar to the create discussion. So we're pretty much just going to do the same thing. We're going to post through to the route here so we can use our route helper posts and store.
04:18
And we need to pass through the discussion in here. So let's say discussion value. Remember, that comes from our use create post composable. And then we want to say, well, when this is successful, what do we want to do?
04:34
Well, we want to reset the form like we did with the discussion form. And then we want to hide the create post form because we don't want that to show after it has been created. OK, let's try this out. And then if there is anything we need to fix up, we can very easily do that and we'll keep our toolbar open just here.
04:52
OK, so let's go ahead and reply and just say, hey, hit reply. And yeah, here we go. Discussion show is not defined. That is a very quick fix.
05:03
Discussions show. And let's just try that one more time. It did work. It just didn't really bring us back.
05:09
So let's say hey again. And there we go. Great. So although it's not scrolling to the post we want at the moment, which is a little bit annoying.
05:17
Also, if this overlaps into the next page, so if this goes over into page two, it's always going to redirect us back to page one. So the user won't be able to see the post that they've just created. But we're going to fix that up later because there's quite a bit of logic and quite a bit of thought that needs to go behind that. But that is now our posts being stored with our new reply form.
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!