TIP 70 / 100

Retrieve the Query Builder After Filtering the Results.

To retrieve the Query Builder after filtering the results: you can use ->toQuery().

The method internally use the first model of the collection and a whereKey comparison on the Collection models.

code
// Retrieve all logged-in users.
$loggedInUsers = User::where('logged_in', true)->get();

// Filter the results with a collection method or PHP callback.
$nthUsers = $loggedInUsers->nth(3);

// A collection cannot perform this database query.
$nthUsers->update(/* ... */);

// Convert the collection back to a query builder with toQuery().
if ($nthUsers->isNotEmpty()) {
    $nthUsers->toQuery()->update(/* ... */);
}