【问题标题】:Laravel remove item in collectionLaravel 删除集合中的项目
【发布时间】:2019-09-19 07:44:08
【问题描述】:

我正在尝试删除我的集合中的一个项目,我正在尝试检查当前集合项的特定值是否等于下一个集合项,如果相等则删除下一个。

    $path = CanvasPath::where('stream_id', $streamId)->get();

    // if in collection $path a value ->path is identical to previous or before remove it
    foreach($path as $key => $value) {
        $collection = $path->getIterator();
        $current = current($collection);
        $next = next($collection);

        if($current->path == $next->path) {
            // remove $next collection
        }
    }

如何删除该项目?

【问题讨论】:

  • 为什么不在它上面应用独特的收藏?
  • @Rahul 嗯,你会怎么做?
  • 你想要与CanvasPath不同的路径??

标签: php laravel collections laravel-5.6


【解决方案1】:

做独一无二的

$path = CanvasPath::where('stream_id', $streamId)->get();

$path = $path->map(function ($array) {
    return collect($array)->unique('id')->all();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多