How to send multiple DB notification same function in laravel 9 code answer ,2024

In Laravel 9, you can send multiple database notifications within the same function by using the send method on the Notification facade multiple times. Here’s an example of how you can do this:

use Illuminate\Support\Facades\Notification;

class SomeController extends Controller
{
    public function sendNotifications()
    {
        $users = User::all();
        $notification = new SomeNotification();

        Notification::send($users, $notification);
    }
}

In this example, the sendNotifications function retrieves all users from the database, creates an instance of the SomeNotification class, and sends the notification to all of the retrieved users using the send method on the Notification facade.

You can also pass an array of notifiables, for example, you can pass an array of User models and an array of Admin models to send notifications to multiple types of users.

use Illuminate\Support\Facades\Notification;

class SomeController extends Controller
{
    public function sendNotifications()
    {
        $users = User::all();
        $admins = Admin::all();
        $notification = new SomeNotification();

        Notification::send([$users,$admins], $notification);
    }
}

This will sends the same notification to all the users and admins.

You can also use the sendNow method to send the notifications immediately, instead of queuing them to be sent later.

Discover more from STRUGGLER KING.COM

Subscribe now to keep reading and get access to the full archive.

Continue Reading