Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After LazyCollection calls the unique method and then calls the isEmpty method, a piece of data will be lost. #38817

Closed
zishang520 opened this issue Sep 15, 2021 · 3 comments · Fixed by #39041
Labels

Comments

@zishang520
Copy link

zishang520 commented Sep 15, 2021

  • Laravel Version: 6.20.34
  • PHP Version: 7.4.2
  • Database Driver & Version:
    mysql 5.7.34-log

Description:

After LazyCollection calls the unique method and then calls the isEmpty method, a piece of data will be lost.

Steps To Reproduce:

// The offending code snippet:
$items = \Illuminate\Support\LazyCollection::make(fn () => yield from [['id' => '1'], ['id' => '2'], ['id' => '4']]);
dump($items->toArray()); // Output: [['id' => '1'], ['id' => '2'], ['id' => '4']]
$unique_items = $items->unique('id');
// If the output is here, there is no problem, but `LazyCollection::isEmpty()` will return true.
// dump($unique_items->toArray()); // Output: [['id' => '1'], ['id' => '2'], ['id' => '4']]
if (!$unique_items->isEmpty()) {
    // When I judge whether it is a null value, I lost a value.
    dump($unique_items->toArray()); // Output: [['id' => '2'], ['id' => '4']]
}

// Everything is normal for this part of the code.
$items = \Illuminate\Support\Collection::make([['id' => '1'], ['id' => '2'], ['id' => '4']]);
dump($items->toArray());
$items = $items->unique('id');
if (!$items ->isEmpty()) {
    dump($items ->toArray());
}
@driesvints
Copy link
Member

@JosephSilber I guess this works as expected but wanted to confirm with you.

@JosephSilber
Copy link
Member

JosephSilber commented Sep 30, 2021

I guess this works as expected

It does not. It's broken.

You can see the problem more easily with this piece of code:

$items = LazyCollection::make(function () {
    yield 1;
    yield 2;
    yield 3;
});

$items
    ->unique()
    ->tap(fn ($collection) => $collection->take(2)->eager())
    ->dd(); // [3]

That call to take(2)->eager() consumed the first 2 items, so the subsequent enumeration is missing that.

That's not expected. Separate enumerations should never affect each other.


I'll work on a fix.

@driesvints
Copy link
Member

Thanks @JosephSilber

JosephSilber added a commit to JosephSilber/framework that referenced this issue Sep 30, 2021
victorvilella pushed a commit to cdsistemas/framework that referenced this issue Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants