Php 在Laravel 5.5中向用户发送通知

Php 在Laravel 5.5中向用户发送通知,php,laravel,notifications,laravel-5.5,Php,Laravel,Notifications,Laravel 5.5,情况就是这样。我让用户A通过通知发送给其他用户B、C、D。。。加入一个组的请求。因此,在laravel中,我创建了迁移和控制器来处理通知 这是GroupController的代码 ... foreach ($userINList as $userIN) { $userIN = str_replace(' ', '', $userIN); $userDBList = User::all(); foreach ($userDB

情况就是这样。我让用户A通过通知发送给其他用户B、C、D。。。加入一个组的请求。因此,在laravel中,我创建了迁移和控制器来处理通知

这是GroupController的代码

...

foreach ($userINList as $userIN) {
            $userIN = str_replace(' ', '', $userIN);
            $userDBList = User::all();
            foreach ($userDBList as $userDB) {
                $name = $userDB->last_name . $userDB->first_name;
                $name = str_replace(' ', '', $name);
                if (strcmp($name, $userIN) == 0) {
                    $newGroup->users()->attach($userDB->id, ['role' => 'member', 'state' => 'pending']);

                    $notification = User::find($userIN->id);
                    $notification->notify(new GroupNotification($newGroup));

                }
            }

        }

...
因此,在
$notification
中,我将尝试传递接收邀请的用户的id,然后使用notify()方法发送通知,但在用户A创建组后,没有通知给用户B、C、D。。。 我已经将
使用应报告的
纳入了集团模式。那有什么问题?我必须做的事


谢谢

从代码中我可以看出,您正在执行以下操作:

  • $userINList
    变量中有一个名称数组
  • 循环遍历数组中的每个名称
  • 删除名称中的所有空格
  • 检索每个
    用户
  • 循环浏览每个
    用户
  • 删除
    用户
    名称中的所有空格
  • 比较这两个名字
  • 如果比较通过,则将
    用户添加到组中并发送通知
  • 我们可以在这里做很多改进。例如,我们已经知道您希望通知哪些用户,因此您不需要获取和比较所有用户

    首先,
    $userINList
    应该是
    User
    对象的数组,或者是
    User
    id
    s的数组-一个
    User
    对象的数组更好。然后您可以简单地遍历每一个

    例如,如果您有一个ID数组,则可以执行以下操作:

    $group = Group::find(1);
    $userINList = [1, 2, 3, 4];
    
    User::whereIn('id', $userINList)
        ->get()
        ->each(function ($user) use ($group) {
            $group->users()->attach($user->id, [
              'role' => 'member',
              'state' => 'pending'
            ]);
    
            $user->notify(new GroupNotification($group));
        });
    
    $group = Group::find(1);
    
    collect($users)->each(function ($user) use ($group) {
        $group->users()->attach($user->id, [
            'role' => 'member',
            'state' => 'pending'
        ]);
    
        $user->notify(new GroupNotification($group));
    });
    
    如果你有一个对象数组,那就更容易了,你可以这样做:

    $group = Group::find(1);
    $userINList = [1, 2, 3, 4];
    
    User::whereIn('id', $userINList)
        ->get()
        ->each(function ($user) use ($group) {
            $group->users()->attach($user->id, [
              'role' => 'member',
              'state' => 'pending'
            ]);
    
            $user->notify(new GroupNotification($group));
        });
    
    $group = Group::find(1);
    
    collect($users)->each(function ($user) use ($group) {
        $group->users()->attach($user->id, [
            'role' => 'member',
            'state' => 'pending'
        ]);
    
        $user->notify(new GroupNotification($group));
    });
    

    超级简单:-)

    我对您的第一个示例做了一些修改,现在一切都很好。非常感谢:)