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
24. Adding auth specific filters

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've got a solid base to build our filters on, let's go ahead and implement some authenticated specific filters. So what we're going to do with this is just go ahead and grab the unordered list here,
00:11
paste it down here, and then only show this if the user is currently authenticated. We can do that with page, props, auth, and user. So this will only be shown when we're signed in. So if we give that a refresh at the moment, of course, we are not signed in.
00:29
And yeah, we actually get a 404 here. Now, this is really interesting. The way that we've set this up is we want the discussion to be passed in to our roots just here, and that's not quite going to work
00:40
because this kind of can mean anything can be passed in. And technically, what we're doing by clicking on login is looking for a discussion with a slug of login, which doesn't make sense. So for now, let's just go ahead and add in the discussions prefix to this just to fix that up.
00:54
And let's go ahead and sign in to see them auth specific filters. So good catch of a bug there. So we'll space this out with some styling. First of all, if we head over to our navigation,
01:04
let's go ahead and set a border on the top for this. And we'll set the border on the top color as gray 500, maybe. Or let's dial that down a bit to 100. And then we're going to set a padding on the top to three.
01:17
That should nicely space this out. What we can also do is space out these items just here as well. So let's say a space Y of three. And there we go.
01:30
So the two filters that we're building here are mine. So my discussions. So let's change this over to my discussions. And we'll go ahead and set the filter in here to mine and just one.
01:43
And then we'll grab this href here. And let's go ahead and change this to participating. And this is going to be participating. So things that we have replied to.
01:58
Let's change that over. And we should be good. The only thing we need to do is just build these out now. So the query here needs to change.
02:08
So let's change this over for this one. Because we just copied and pasted this down. And this needs to be mine. And this needs to be participating.
02:18
And we should be good. Great. So if we click on my discussions. Of course, at the moment, it's not allowed because we haven't built one.
02:24
But eventually, that should just work once we have built the filter. So over to the forum index controller, we're going to add these two filters to here. All we need to do, of course, is just go over to our query filters, copy this over, build a new filter, and then add it to the list.
02:41
So I'm going to call this mine query filter. And let's paste this in and change the name. And then we just need to figure out how to filter this down. Now, if the user isn't available and we have actually chosen this filter,
02:58
which is completely possible, then we don't want to do anything. So if that's the case, we're just going to return the builder back or return the query in this case back. Otherwise, we're going to go and just say query where belongs to
03:16
and pass in the currently authenticated user. In fact, for here, we can actually just return because we don't need to do anything with this. OK, so now that we've done this, let's go ahead and add this to the forum index.
03:27
Let's paste this down here, mine query filter, and we'll set that to mine. And if we head over here, sure enough, over in my discussions, this now works. And of course, I have created these two discussions. So in the database, let's just change this around a little bit.
03:43
So the discussion need help with view. Change that over to Mabel. And then we'll go ahead and change the post over as well. So this one here to Mabel.
03:55
Save that out. Go over to the browser, give that a refresh. And of course, under my discussions, we only see one. All discussions, we see both of them.
04:03
So that's great. Now, let's just go ahead and modify the pagination because we don't need that to be so low anymore. And we'll go ahead and fill out the participating filter,
04:14
which again is not allowed at the moment. This one's a little bit more complex. Let's again just go ahead and copy one of these over. So we'll copy the mine query filter to participating query filter.
04:28
And let's go ahead and paste that in. And of course, just change the name over participating. And let's register this over here so we can start to use this straight away. So participating query filter and participating.
04:42
Great. OK, so the same rule applies here. We don't want to do anything if the user isn't currently authenticated. And now what we want to do is for each of the discussions that we go through,
04:54
we first of all want to make sure it has posts. And then into that, we want to use a closure to further drill down. So the first thing we want to do is say where has posts. This is going to allow us to go into these posts
05:08
and start to perform queries on the actual posts themselves. The query for this is going to be query where belongs to user. So it will be the currently authenticated user in this case. So that is pretty much just going into the post and saying,
05:23
does this discussion have any posts which belong to the currently authenticated user? Now, there's one more thing we need to add to this as well, which is really, really important, because on the surface, it might seem like this is going to work really nicely.
05:36
But if we head over to participating, this is giving me my topic that I've just created. So it's really no different from my discussions. Of course, I've participated in this because I've created the first post.
05:50
What we want to do is add a clause to this to make sure that we don't actually own the discussion. So for this, what we can do is on the original query, which, of course, relates to the discussions,
06:00
we can not really say where not belongs to, because that doesn't actually work. And that will end up looking for a column called not belongs to, which doesn't obviously exist.
06:13
So in this case, we can just say something like where user ID doesn't equal and the auth ID. So the currently authenticated user's ID. And that's a method. And that will just exclude any discussions that we have created.
06:31
So let's give this a refresh. And now participating is empty. My discussions shows us the correct thing. And of course, all discussion still shows us everything.
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!

Comments

No comments, yet. Be the first to leave a comment.