【问题标题】:How to access items within a collection map without breaking a fluent chain?如何在不破坏流畅链的情况下访问集合地图中的项目?
【发布时间】:2017-03-08 08:44:43
【问题描述】:

如果你有一个简单的map 使用 Laravel 集合,你可以通过执行以下操作轻松访问基本集合:

$items = [ "dog", "cat", "unicorn" ];
collect($items)->map(function($item) use ($items) {
    d($items); // Correctly outputs $items array
});

如果使用带有过滤器/拒绝的流畅链,$items 不再代表项目集:

$items = [ "dog", "cat", "unicorn" ];
collect($items)
    ->reject(function($item) {
        // Reject cats, because they are most likely evil
        return $item == 'cat'; 
    })->map(function($item) use ($items) {
        // Incorrectly (and obviously) outputs $items array (including "cat");
        // Would like to see the $items (['dog', 'unicorn']) here
        d($items);

        // Will correctly dump 'dog' on iteration 0, and 
        // will correctly dump 'unicorn' on iteration 1 
        d($item); 
    });

问题

是否可以访问修改后的项目数组,或者访问当前状态的集合。

Javascript 中的类似库,如 lodash,将集合作为第三个参数传入 - Laravel 集合没有。

更新/编辑

明确地说,我可以做类似的事情(但它会破坏链条)。我想做以下事情,但没有集合的中间存储。

    $items = [ "dog", "cat", "unicorn" ];
    $items = collect($items)
        ->reject(function($item) {
            // Reject cats, because they are most likely evil
            return $item == 'cat'; 
        });

    $items->map(function($item) use ($items) {
            // This will work (because I have reassigned 
            // the rejected sub collection to $items above)
            d($items);

            // Will correctly dump 'dog' on iteration 0, and 
            // will correctly dump 'unicorn' on iteration 1 
           d($item); 
        });

【问题讨论】:

    标签: php laravel laravel-5 laravel-collection


    【解决方案1】:

    当您在 map() 内执行 d($items); 时,它指的是您的原始数组。如果你在map() 中执行var_dump($item),你会看到它只输出dogunicorn

    $items = [ "dog", "cat", "unicorn" ];
    $newItems = collect($items)
        ->reject(function($item) {
            // Reject cats, because they are most likely evil
            return $item == 'cat';
        })->map(function($item) use ($items) {
            var_dump( $item );//TODO
        });
    
    var_dump( $newItems );//TODO
    

    【讨论】:

    • 我可能不清楚 - 我会更新这个问题。我希望在每次迭代中访问整个(可能会减少)集合
    【解决方案2】:

    您可以通过运行 $this->all() 之类的命令来访问集合的当前状态。

    $items = collect(["dog", "cat", "unicorn"])
        ->reject(function($item) {
            return $item == 'cat'; 
        })
        ->map(function($item) {
            dd($item); // current item (dog/unicorn);
            dd($this->all()); // all items in the collection (dog and unicorn);
        });
    

    【讨论】:

    • 确定吗? $this 指的是当前对象/实例(是的,可以访问items)但是,$items 仍将是原始的未修改版本。反正这是我的理解
    • 是的,我确定。由于 Laravel 集合是不可变的,$this 指的是(在这种情况下)不包含 'cat' 的集合,因为它在上面的操作中被拒绝了。 $items 变量指向一个集合,该集合是集合管道中所有操作的结果。
    • 我很欣赏这个答案,但$this 指的是当前上下文——例如:gist.github.com/cjke/fa0e152b5d57da41e74e5027446984d4 $this 将指的是RouteServiceProvider
    • 哦,是的。那是我的错误。对不起
    【解决方案3】:

    你可以这样做。

      return collect($models->items())->each(function ($item, $index){
            if( !$item->quoteReturnDetail){  echo  $item ;  exit(); }   });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多