Sending notifications when a user is mentioned
Every time a user is mentioned, the Kirschbaum\Commentions\Events\UserWasMentionedEvent is dispatched. Commentions ships an optional, opt-in notification you can enable via configuration, or you can listen to the event and handle it yourself.
Example usage:
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Notifications\UserMentionedInCommentNotification;
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
class SendUserMentionedNotification implements ShouldQueue
{
use InteractsWithQueue;
public function handle(UserWasMentionedEvent $event): void
{
$event->user->notify(
new UserMentionedInCommentNotification($event->comment)
);
}
}
If you have event auto-discovery, this should be enough. Otherwise, make sure to register your listener on the EventServiceProvider.
Built-in opt-in notifications
Enable notifications for mentions in your config/commentions.php:
'notifications' => [
'mentions' => [
'enabled' => true,
'channels' => ['mail', 'database'],
],
],
Optionally, provide a URL resolver so emails/links point users to the right place:
use Kirschbaum\Commentions\Config;
Config::resolveCommentUrlUsing(function (\Kirschbaum\Commentions\Comment $comment) {
// Return a URL to view the record and scroll to the comment
return route('projects.show', $comment->commentable) . '#comment-' . $comment->getId();
});