I think in your second example it's down to what you did before your code snippet in order to create $model
if you fetched the model (eg with find) then you are now working on, and sorting, a collection and not a query.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is there a way to set multiple orderBy criteria when querying from a model without chaining the commands.
Chaining (Works)
$result = $model->orderBy('name', 'asc')->orderBy('id', 'asc')->paginate(10);
No Chaining (Does not work)
$model->orderBy('name', 'asc');
$model->orderBy('id', 'asc');
$result = $model->paginate(10);
I'm receiving the sort criteria from table headers so chaining becomes difficult since the sort criteria changes (number of sort fields).
How is this handled generally?
I discovered this on one of Jeffrey's videos that shows how to handle this outside of the model.
$model = (new Model)->newQuery();
foreach ($request->get('sort_criteria') as $column => $direction) {
$model->orderBy($column, $direction);
}
$results = $model->paginate(10);
Please or to participate in this conversation.