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

lazyById doesn't check availability of id (alias) column in database response and silently ends up with endless loop. chunkById does. #48436

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

decadence
Copy link
Contributor

@decadence decadence commented Sep 18, 2023

lazyById doesn't check availability of id (alias) column in database response and silently ends up with endless loop if DB query has more than 1 page of results. It happens if we don't select alias column.

If we use this code (we don't select id column):

// select without id
$users = User::select("name")->lazyById(10);

foreach($users as $user) {
    // endless loop (generator) because lazyById will always return elements from the first page
}

So foreach will fall into endless loop and never ends if there are more than 1 page of results.

You can say that it's programmer's responsibility to include id in select but chunkById has this check and lazyById documentation doesn't say anything about id is required.

In the chunkById we have this code which verifies that response has id column.

$lastId = data_get($results->last(), $alias);

if ($lastId === null) {
    throw new RuntimeException("The chunkById operation was aborted because the [{$alias}] column is not present in the query result.");
}

It seems logical to me to add such a check to lazyById as well.

Initial issue: #48401

@taylorotwell taylorotwell merged commit 39bad03 into laravel:10.x Sep 18, 2023
21 checks passed
@decadence decadence deleted the patch-1 branch September 18, 2023 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants