This episode is for members only

Sign up to access "Laravel Mentions" right now.

Get started
Already a member? Sign in to continue
Playing
07. Detecting pivot events

Transcript

00:00
So whenever someone is mentioned in a comment or elsewhere, we want to send a Laravel notification. So that might be an email, a slack, an SMS, it doesn't really matter, but we want to do that
00:13
somewhere. The question is, where do we do that? Do we, for example, want to take this bunch of users and then go ahead and notify each of them? For example, this wouldn't actually work, but we could take them like users each and notify and then send a new notification in here, if we haven't cast this to an array and just plucked out the ID. Well, if you think about it,
00:37
by doing that, what you're doing is you're notifying the users every single time we update a comment, which could involve submitting through and containing a previously used ID or a previously used user or mentioned user. So what we want to do is we want to create an event when a pivot is attached and when a pivot is attached only. We don't want to
01:04
notify these users every single time because it's just not going to work. So we're writing a test for this shortly, but let's go ahead and get this working. So to do this, Laravel by default does not support listening to attached pivot events. So what we've done here is we've created our comment observer, which using the created and
01:26
updated eloquent events, finds and syncs our mentions. But what we can't do is create out an eloquent event for when a pivot, e.g. for when any of these get created, it just won't work. So how do we do this? Well, we go ahead and use a package, and I've used this before. This is actually used directly on the CodeCourse website, so it's pretty reliable.
01:50
Okay, so let's go ahead and install this and then I'll show you how this works. So let's grab the install command for this and go ahead and pull it into our project. And I don't think there's too much more setup we need to do here. We just need to pull this pivot event trait into the model that we want to work with. So that is the comment.
02:09
So let's go over to our comment and go ahead and pull this in to our project and just pull the namespace from the package just up here. Okay, so what do we want to do here? Well, we can't really do this in an observer, so we're going to go ahead and create out a static booted method, which is where we would usually do something like this, static created.
02:34
We've done this stuff here inside of a server. So what we do instead is now that we've got this package pulled in and we've got this trait added to our comment, we can say pivot attached. So what this will do is it will detect whenever a pivot, e.g. the comment mentioned gets attached. So let's pull in the closure here for this. Now what we get into here is the model
02:56
that we're actually working with here. Then we get the relation name, which is really important. And then we get the pivot IDs. So let's just check each of these out just to see what each of these do. So let's dump out the model first. We'll do each of these as we go. So let's say, hey, Alex, and we'll hit post. And there we go. So that's the model itself,
03:18
the actual comment that's been posted, pretty straightforward. Next is the relation name, because if we think about it, if we apply this functionality to a model, we could have multiple different pivots for a single model. So we need to know which relation we're dealing with here. So in our case, when we do mention someone, it's the mentions relation that has been triggered,
03:42
that's triggered this pivot attached event. So next, and really importantly, we have the pivot IDs. So let's now mention two people. So let's say Alex and Tabby, and let's hit post and we get, well, we get one here, but the next time around, we're going to get the second one. So we'll see that in action in just a second. But now at least we know that this is working.
04:06
So what we want to do is notify the user down here, but we only want to do this if the mentions have been attached. So all we need to do here is just check the relation name and make sure it's mentions. And then here, we go ahead and notify the user. So I'm just going to say die dump and notify. Let's go ahead and just try this out by saying, hey, hit post, nothing happens because
04:33
nothing's been attached. And we can say, hey, Alex, hit post and notify. So this is the point where we want to notify the users or the individual user. Okay, so now that we have this in here, and we know that this is functioning, let's go over to the next episode and actually send the comment mention notification.
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!