0

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

我有用户 -$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));

感谢您的任何帮助,您可以提供。

4

1 回答 1

0

您可以将集合与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));
于 2020-12-07T23:53:59.240 回答