Skip to content

chunkById/forPageAfterId doesn't work with orderby #35400

@LastDragon-ru

Description

@LastDragon-ru
  • Laravel Version: 8.15.0
  • PHP Version: 7.4.x
  • Database Driver & Version: Mysql 8

Speps to reproduce:

Route::get('/test', function () {
    /** @var \DateTimeImmutable $date */
    $date = \Illuminate\Support\Facades\Date::now();

    \App\Models\User::factory()->create([
        'created_at' => $date,
    ]); // id = 1
    \App\Models\User::factory()->create([
        'created_at' => $date->subDays(1),
    ]); // id = 2
    \App\Models\User::factory()->create([
        'created_at' => $date->subDays(2),
    ]); // id = 3

    \App\Models\User::query()
        ->orderBy('created_at')
        ->chunkById(1, function (\Illuminate\Support\Collection $users) {
            var_dump($users->pluck('id')->all());
        });
});

Actual results:

array (size=1)
  0 => int 3

Expected Result

array (size=1)
  0 => int 1

array (size=1)
  0 => int 2

array (size=1)
  0 => int 3

Description

This happens because chunkById/forPageAfterId uses:

public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
{
$this->orders = $this->removeExistingOrdersFor($column);
if (! is_null($lastId)) {
$this->where($column, '>', $lastId);
}
return $this->orderBy($column, 'asc')
->limit($perPage);
}

probably it should remove all other "order by"s instead (= call reorder()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions