Laravel 条件通知

Laravel Conditional Notifcations

我正在尝试根据多种条件向多个用户发送通知。如果满足一个或多个条件,我想将这些用户添加到将发送通知的用户数组中。问题是如何将每个条件中的所有用户添加到最终数组中?

我有用户 - $data['salesman'] 在我的例子中 - 从输入中添加。然后我有更多的用户我想添加到基于表单的 $data['branch'] 字段的静态数组。

            // Find selected salesman to send notifications
            $users = User::whereIn('name', $data['salesman'])->get();

            // Send to appropriate people at selected branch
            if($data['branch'] === 'Richmond') {
                $users = User::whereIn('name', ['Jane Doe', 'John Doe'])->get();
            }

            // Send notification to proper user(s)
            Notification::send($users, new TrafficCreated($traffic));

感谢您提供的任何帮助。

您可以使用 merge 方法合并集合。

类似于:

$users = User::whereIn('name', $data['salesman'])->get();

if($data['branch'] === 'Richmond') {
    $branch_users = User::whereIn('name', ['Jane Doe', 'John Doe'])->get();
}

// todo: add other conditions for different branches here

if (isset($branch_users)) {
    $users->merge($branch_users);
}

Notification::send($users, new TrafficCreated($traffic));