-
app/Notifications/YouWereMentionedNotification.php
Open in GitHubuse App\Models\Comment; use Illuminate\Notifications\Notification; class YouWereMentionedNotification extends Notification { public $comment; public function __construct(Comment $comment) { $this->comment = $comment; } public function via($notifiable) { return ['database']; } public function toArray($notifiable) { $topic = $this->comment->commentable; $text = __(':creator mentioned you in :topic', [ 'topic' => $topic->title, 'creator' => $notifiable->name, ]); $url_prefix = get_class($topic) == 'App\Models\Task' ? 'tasks/' : 'leads/'; return [ 'assigned_user' => $notifiable->id, 'created_user' => $this->comment->user_id, 'message' => $text, 'type' => get_class($topic), 'type_id' => $topic->id, 'url' => url($url_prefix . $topic->external_id), 'action' => 'mentioned' ]; } }
-
app/Listeners/NotiftyMentionedUsers.php
Open in GitHubuse App\Models\User; use App\Events\NewComment; use App\Notifications\YouWereMentionedNotification; class NotiftyMentionedUsers { public function handle(NewComment $event) { collect($event->comment->mentionedUsers()) ->map(function ($name) { return User::where('name', $name)->first(); }) ->filter() ->each(function ($user) use ($event) { $user->notify(new YouWereMentionedNotification($event->comment)); }); } }