duanmei1350 2017-05-20 06:18
浏览 81
已采纳

从集合中删除集合

I've been building a custom ticket system for my Laravel application, and users can place comments on their tickets.

When a new comment is placed, I want to send a notification to everyone involved in the ticket.

Users can be involved if they are:

  • the ticket owner
  • the agent assigned to the ticket
  • invited as a participant to the ticket

To do this, I am creating a collection of users and then looping through them to notify them. The only issue with this is that it currently includes the person making the comment too, and they don't need to be notified as they were the ones leaving the comment.

I've tried to filter the collection to remove the user if the id matches the currently logged in user, but this doesn't seem to work:

$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

Upon further reading, I think this is because you use filter to remove an element from a collection, rather than a collection that is inside a collection.

How can I remove a user from the collection if they are the currently logged in user?

When I dd($toBeNotified) after running the above, this is the result:

enter image description here

  • 写回答

1条回答 默认 最新

  • drwkqwa82140 2017-05-20 06:37
    关注

    You can use except to achieve this.

    $toBeNotified = $toBeNotified->except(auth()->id());
    

    As a side note you should use merge when you want to add more than one user.

    $toBeNotified = $toBeNotified->merge($ticket->activeParticipants);
    

    The filter method you use is correct as well, but it returns the filtered collection while keeping the original collection unchanged.

    $toBeNotified = $toBeNotified->filter(function($user) {
        return $user->id != auth()->id();
    });
    

    Edit: The except would work only when you have an eloquent collection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么