Laravel移除集合中的物品

时间:2019-09-19 07:44:08

标签: php laravel collections laravel-5.6

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

    $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
        }
    }

如何删除该项目?

1 个答案:

答案 0 :(得分:0)

做独特

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

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