【发布时间】:2017-05-20 06:18:23
【问题描述】:
我一直在为我的 Laravel 应用程序构建一个自定义票证系统,用户可以在他们的票证上放置 cmets。
当有新评论出现时,我想向参与票证的每个人发送通知。
用户可以参与,如果他们是:
- 票的所有者
- 分配给工单的代理
- 被邀请作为门票的参与者
为此,我创建了一个用户集合,然后循环访问他们以通知他们。唯一的问题是它目前也包括发表评论的人,他们不需要通知,因为他们是发表评论的人。
如果 id 与当前登录的用户匹配,我已尝试 filter 集合删除用户,但这似乎不起作用:
$ticket = App\Ticket::findOrFail(1);
//Create collection to hold users to be notified
$toBeNotified = collect();
//Add the ticket owner
$toBeNotified->push($ticket->owner);
//If an agent is assigned to the ticket, add them
if(!is_null($ticket->assigned_to)) $toBeNotified->push($ticket->agent);
//Add any active participants that have been invited
$ticket->activeParticipants()->each(function($participant) use ($toBeNotified) {
$toBeNotified->push($participant->user);
});
//Remove any duplicate users that appear
$toBeNotified = $toBeNotified->unique();
//Remove the logged in user from the collection
$toBeNotified->filter(function($user) {
return $user->id != Auth::user()->id;
});
//...loop through each user and notify them
进一步阅读,我认为这是因为您使用filter 从集合中删除元素,而不是在集合中的集合。
如果用户是当前登录的用户,我如何从集合中删除用户?
当我dd($toBeNotified)运行上面之后,结果是这样的:
【问题讨论】:
标签: php laravel collections laravel-5.2