🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page
  • Scenario
  • Solution
  • Use with Jetstream Teams function

Was this helpful?

  1. Laravel

Send Notification to all team members

When one email should be sent using routeNotificationForMail

PreviousCreate Guaranteed Unique Invoice Number in LaravelNextProtect Staging site with Basic Auth

Last updated 2 years ago

Was this helpful?

Scenario

A team receives a single notification, for instance as a single slack message, but the email version needs to be sent to all members of the team.

Looping over the notification for each member would send individual emails, but would also send multiple slack messages.

Solution

Add the use Notifiable trait to the Team model

Add a method routeNotificationForMail to the Team model

    public function routeNotificationForMail($notification)
    {
        return $this->users->pluck('name','email');
    }

This function returns a collection of recipients with the user email as the key and the user name as the value.

The Notification, sent by email will be [To] the complete list of team members rather than as individual emails.

An advantage of this approach is that all team members can see that the email was sent to them all.

The method routeNotificationForEmail is covered in the docs, but it is not clear how this should be used to return multiple recipients .

Use with Jetstream Teams function

When using the team owner is not returned by the $team->users() relationship. To obtain the full list including the owner, use the allUsers() function.

    public function routeNotificationForMail($notification)
    {
        return $this->allUsers()->pluck('name','email');
    }

You can discuss pages on this site at

https://laravel.com/docs/8.x/notifications#customizing-the-recipient
Jetstream Teams
https://github.com/snapey/talltips/discussions