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
52. Indexing users for mentioning

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 we spoke in the last episode that all of these items need to come from somewhere.
00:04
They can't be hardcoded and we don't want to load in every single user into an array within the front end. That will just have a massive performance impact. So what we want to do is ideally load all of our users into MeliSearch in a separate index with their username and the label that we want to see over here. So basically we want to end up
00:25
with an index with a label and a value which is searchable for every single user. That way what we can do is using mentionable hook into the event. So when we type we're actually sending a request to MeliSearch which is very very fast to search for our users. So let's not get ahead of ourselves. Let's first of all figure out how we're going to
00:47
index this data into MeliSearch. Now we could and our first thought might be to hop over to the user model and go ahead and do what we did on our discussion and go ahead and use searchable. Now we could do that but the only issue with this is we don't really want the user searchable. We might later on but ideally we'd want a completely separate index just for user mentions. That would
01:12
keep things really clean and also really secure. We don't want to really index all of our user data. So what we're actually going to do is we're going to create out an entirely new model because Laravel Scout deals with models. We can't very easily and very cleanly index our own data and create our own indexes. So let's go ahead and create a model called user mention. So what this
01:34
is going to do is practically use the same table. It's going to look exactly the same as the users table but by having a separate model here for Laravel Scout that's going to allow us to control how we add this data in. So with this we're going to go ahead and actually set the table here to users. Now the reason that we do that is because within Laravel if we have user mention
02:00
that's going to look for like a user underscore mention table or something similar. So we're basically just creating a user model. We could if you think extend the user but let's try and keep this as simple as possible. So we want to use the searchable trait in here because we want this to be searchable and much like our discussion we want to make sure that we only index certain data.
02:25
So let's go and first of all actually tell Laravel Scout how we want this to be indexed as in the index name. So for that we use the searchable as method. So let's say users mentions and let's choose the data and really importantly the structure that we want this to be searchable as. So to searchable array and let's go ahead and return. This time we will use an array because we
02:56
need to build this up slightly differently. For the value that's just going to be the user's username. That's the thing that gets searchable on the client side. The label can be whatever we want. So in this case let's say the user's name and then let's append on some parentheses. Inside of these parentheses let's have an at and then let's append on their username like so. So it's basically
03:23
look like what we had before like this. Let's get rid of that semicolon and we should now be able to run Scout on this and see this indexed. So let's go ahead and do that now. php artisan scout import app models and user mention and let's see what we get. Okay so over to MeliSearch click on this and there we go. So now we have pretty much the structure that we would expect
03:50
here and that we need for our mentionable functionality but now inside of MeliSearch. Now we are pretty much done but there's one issue here. Let's go over to my profile section here. Now my username at the moment is AlexGS. Let's just check who else we've got in here. Let's say I want to change my username to Alex. Now I'm going to go ahead and do that now. Save that out
04:15
and let's have a look. Well inside of here the old value is still hanging around which is not good because if I do change my username that's going to be shown everywhere on the forum and then when people go ahead and search for my username they're going to be a bit confused as to why this is different. So let's set that back to AlexGS and look at how we can
04:35
manually index the user, the specific user, when anything like that changes. So to do this we're going to use a model observer so when the user is updated we can actually change the username around. So let's go ahead and make an observer here and we'll call this user observer. There's a load of ways to observe eloquent events within Laravel. In fact we already did this once over on the
05:00
discussion where we looked for or checked for when a particular discussion is being created. So you could do this directly inside of your user table but I'm going to go ahead and just use an observer. I just find it easier to find these things. So let's open up the user observer and in here we just want to create out a method that corresponds to the name of the eloquent event.
05:25
For us that's going to be when this user is updated. So let's create out an updated event here. We'll get the user into this so we can do something with them and what we can do within Laravel Scout is we can manually index a specific record. Now the problem here is we're getting through a user and not a user mention so we can't say something like user and then the method we
05:49
want is searchable. That will trigger an index over to Mellaserch or whichever software that we're using for search but this is not the mention this is the user itself which as we know does not have a searchable trait. So what we'll actually need to do is over on the user model set up a relationship here that hooks up to the user mention which as we know is a completely separate
06:14
model but it uses the same table. So let's go ahead and create out a mention relationship here and let's go ahead and say that this has one user mention and it's hooked up only by the id because it's the same thing as the user it's just a separate model for Laravel Scout. So a user has a mention which basically represents their mention and now we can say user mention searchable and
06:45
that will now trigger a search or index for the user mention and it should update. So lastly we just need to hook up the observer that we've just created with our model and we can do that over in the event service provider. Again there are loads of different ways to do this but a really good way to do this I don't think it's here by default is to create out an observers
07:13
property in here and then what we can do is give the class name that we want to register observers for and there could be multiple and we give the fully qualified class name to the user observer. So now when the user gets updated we find the mention model and we make that searchable e.g. we update it in our index. Let's try this out by setting my username back to
07:40
Alex and let's head over and let's go down and there it is so it's been updated in our index. Great now the other issue we have which isn't immediately obvious is if we log out here and we register a new account so yeah we've got a little issue there with some authorization which we'll look at later but if we register a new account so let's go over here and let's say abc we'll just
08:08
keep this really simple. What's not going to happen is if we head over to our meta search index we're not going to have that in there. Why? Because we are creating a user not a user mention so what we also want to do is when a user is created we also want to manually create a record for their mention inside of that index. So let's try this one more time I'm going to go ahead and
08:41
log out head over to register def and let's go ahead and see and this should now that we've added that eloquent event in and we open up meta search we should see it in there great. So now when user's registered they automatically get put into that user mention index which we can then use to search on the client side and when they update their username that also gets updated as well.
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.