This episode is for members only

Sign up to access "Laravel Mentions" right now.

Get started
Already a member? Sign in to continue
Playing
05. Mention relationship and syncing

Transcript

00:00
Okay, so now that we are finding all of the usernames with our regular expression matching, now we need to get these synced in the database so we know which comments have which user, which user associated, not just the username. So what we're going to do is go through the process
00:16
of looking up all of the users by the mentions that we've got in here, obviously making sure they actually exist, and then hooking them up in the database. So what do we need to do? Well, the first thing is the relationship between the comment and the user, basically the mention. So let's say phpArtisan make migration, and let's create out a comments mentions table. So we're
00:44
not adding this in the standard Laravel way. The standard Laravel way here would be comment user. We're specifically creating this pivot table as a mentions. So let's go ahead and add this in. So create comments mentions table. What do we want to do? Well, we want to do what every other pivot table in Laravel pretty much does. We want to hook up a comment id, which we
01:11
can constrain here to a user id in here as well, like so. So we now will take a bunch of usernames like Alex, Tabby, we'll look them up in the users table, and we'll hook the user id up with the comment id that they've been mentioned in. So let's go ahead and run phpArtisan migrate here, and let's create the relationship. So over in our comment, this is where we'll get a bunch of
01:42
mentions. So let's create out a mentions relationship in here, and we're going to say belongs to many, because it's a many-to-many relationship with a pivot table. And who gets mentioned? Well, it's a user, and of course, depending on what you're building or how you're structuring this, whatever it is you're mentioning, it might be a completely different thing that
02:04
is getting mentioned. For example, if you were working with something similar to GitHub, where you could mention specific issues with a hash symbol, you might have belongs to many issue. It's entirely up to you what you do here. Okay, so because we don't have the standard name for that pivot table, which remember would normally be comment underscore user based on what we're
02:23
hooking up, we're going to specify this in here. So we're going to say comment underscore mentions, and we kind of want to know when these users were mentioned, so we'll fill in the timestamp as well. Okay, now we've got this relationship, let's try and hook this up in our comment observer. The first thing we want to do is actually check whether there are any mentions.
02:44
So if there aren't any mentions, there's no point even going ahead and syncing. So we're going to go ahead and count up the mentions, which is now an associative array or a multi-dimensional associative array. And we're going to check that for a zero count, and we're just going to return. That's pretty much it. So don't bother even syncing if there aren't any at mentions within
03:05
this comment. Next, we'll take the comment model, we'll access the mentions relationship, and we'll go ahead and sync this up. So what are we syncing here? Well, we want to grab a bunch of user IDs. So let's imagine that we mentioned Alex and Tabby. Now let's imagine that Alex has a user ID of one, which I think he does in the database, and Tabby has an ID of two. Well, we want to find these two
03:36
users in the database by their username. We want to grab the IDs, and then we want to sync these IDs with the mentions, because we know that over here what we're doing is we're hooking up the comment to the user ID, so we can access the users that have been mentioned and not just store the usernames. So what do we do? Well, we just basically go into the user model, we have a
03:58
where-in clause, which we can use on the username, and we want to pass through all of the mentions that we have found to look them users up. So in fact, what I'm going to do is I'm just going to die dump on this up here. So let's say users, and we'll pluck them out like this. So we're going to say mentions, but we can't really just pass mentions in because remember the structure of
04:22
this mentions is a little bit strange. We've got a zero key, we've got a username key, and then we've got a one key. So we're going to collect up, so basically create a Laravel collection from these mentions, and we're going to pluck out the username from each of them. So actually what we'll do is let's dump on this first, and then we'll just start to piece this together. So let's dump
04:46
on this first and see what we get. So let's go over and say hey Alex and Taddy, let's hit post, and we now get a Laravel collection with them two items in. Perfect. So we've extracted all of the usernames that have been mentioned. What we can then do is pass them into the where-in clause like this. Let's go ahead and say get on the end of here, just to test this out, and we'll die dump them
05:15
users. We will be writing a test for this as well in the next episode, so we'll see how this comes together. Let's say hey Alex and Tabby again. Now we get two users. Of course we get Alex in there being plucked out by that username, and we get Tabby being plucked out by that username. So that is now working. But how do we sync this data? Well I think you can actually just pass this
05:38
directly in with the models, but I'm going to go ahead and pluck out the ID of the users, and I'm going to cast that directly to an array. I think you can pass a collection through to sync, but we'll just pluck out this to keep things simple. So again I'm just going to die dump on users and show you what this will do now, and that will get back to the point I was making earlier where if we
05:59
mention two users, one with an ID of one, one with an ID of two for example, we get one and two ready to insert in and sync up in the database. So now what we can do is just take this, and we can go ahead and pass this in. Now even if we try and mention someone who doesn't exist, this will still sync, but it just won't sync anyone up. So you don't need to worry about
06:24
checking whether each of these usernames exist first of all. Okay now that we've done this we should be good, so let's go ahead and try this out. So just for clarity, let's go ahead and get rid of all the comments we've posted so far, and go back to this, and let's say hey Alex, hit post, let's go over to the database and look at our comments mentioned, and you can see yeah sure enough
06:46
comment ID of 15 now has user ID of one mentioned. Let's delete this, let's delete this comment, and let's go back and we'll mention two users, Alex and Tabby. Let's mention someone that doesn't exist as well, so I can show you that this still works. There we go, let's go over to the database, and there's our comment, and there are both of the users that do exist now attached to that comment.
07:10
So actually syncing these up in the database like this isn't technically strictly necessary, because we could still alert users that they had been mentioned in a comment without storing them in the database, but this is really useful for later when, for example, you want to give users the ability to see which comments, or posts, or anything that they have been mentioned in. So it's
07:33
always a good idea to store these in the database so we can use them later. Okay, so now that we've got this functionality in here, we do know this works, but we're going to go over to the next episode and write a very simple couple of tests for this to make sure that everything is working as we expect it to.
15 episodes1 hr 52 mins

Overview

Add mentionable functionality to your Laravel applications and mention users, projects, issues… literally anything.

We’ll start by setting up a simple comment system with Livewire, then detect, sync, notify and test mentions step-by-step. Not using Livewire? Don’t worry, the core functionality works with any stack.

If you are using Alpine/Livewire, we’ll add mention support to textareas to get a list of users we’re able to mention when we hit a trigger key.

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

Episode discussion

No comments, yet. Be the first!