Search code examples
phplaravel-5laravel-5.3traits

Use Laravel routeNotifcationForMail() to send to multiple emails addresses?


I'm using the Notifiable trait on my Model.

User has comma-separated list of subscribed emails (string).

The documentation and my background reading suggests that this will only route to a single email address.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the mail channel.
     *
     * @return string
     */
    public function routeNotificationForMail()
    {
        return $this->email_address;
    }
}

Laravel 5.3 Notifications

I've also read Matt Stauffer on Notifications but can't see an answer there.

My NotificationClass->toMail() is as follows.

/**
 * Get the mail representation of the notification.
 *
 * @param Store $store
 * @return mixed
 * @throws Exception
 */
public function toMail(User $user)
{
    return (new MailMessage)->view('notifications.emails.activity-roundup', compact('user'));
}

Solution

  • I guess you want code below since you can send array (see here):

    /**
     * Route notifications for the mail channel.
     *
     * @return string
     */
    public function routeNotificationForMail()
    {
        return $this->emailsToArray();
    }
    
    private function emailsToArray() {
        if (is_null($this->email_addresses)) {
            return $this->email; //default email
        }
        //perform more checks that you need
        return array_map('trim', explode(',', $this->email_addresses))
    }