Skip to content

lazyById doesn't check availability of id column in database response and silently ends up with endless loop  #48401

@decadence

Description

@decadence

Laravel Version

10.22.0

PHP Version

8.1.9

Database Driver & Version

MySQL 5.7.39 (Windows 11)

Description

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.

Steps To Reproduce

Create table with 100 records for example

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions