如何在不破坏流畅链的情况下访问集合图中的项目?

If you have a simple map using the Laravel collection, you can easily access the base collection by doing the following:

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

If using a fluent chain with filters / rejections, $items no longer represents the set of 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); 
    });

Question

Is it possible to access either the modified items array, or alternatively, get access to the collection in its current state.

Similar libraries in Javascript, like lodash, pass in the collection as the third argument - the Laravel collection does not.

Update/Edit

To be clear, I can do something like (but it breaks the chain). I would like to do the following, but without the inbetween storage of the collection.

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

When you do d($items); inside map() it refers to your original array. If you do var_dump($item) inside map() you'll see that it outputs only dog and unicorn.

$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

You can access the current state of the collection by running something like $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);
    });