Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MattB's avatar
Level 2

Order By not working

What have I done wrong here? Is it how I've strung the query together? I have the below query:

 $companies = Category::all()->groupBy('company')->OrderBy('company', 'asc');

Which results in the following error:

Method Illuminate\Database\Eloquent\Collection::OrderBy does not exist.

Have I just done it in the wrong order or something?

0 likes
14 replies
MattB's avatar
Level 2
$companies = Category::all()->groupBy('company')->SortBy('company', 'asc');

Gives this error:

asort() expects parameter 2 to be integer, string given

Where have I gone wrong here?

Nakov's avatar

Take a look at my edit above please and use the database for your query. Also please read the documentation.

sortBy default is asc so no need to specify, there is a sortByDesc instead which accepts only the column through which you want to sort, the same for sortBy, you should have ->sortBy('company')

MattB's avatar
Level 2

But when I remove the asc part, it doesn't sort by at all. I'm now using

$companies = Category::all()->groupBy('company')->sortBy('company');
Nakov's avatar

@mattb read the answers please.. use this instead:

Category::groupBy('company')->orderBy('company', 'asc')->get();

What is your company? Is it an integer field? What data does it contains?

MattB's avatar
Level 2

The solution you gave produces this error:

SQLSTATE[42000]: Syntax error or access violation: 1055 'sonic.categories.id' isn't in GROUP BY (SQL: select * from `categories` group by `company` order by `company` asc)

The company column is a string column. Should there be another way of sorting strings?

Nakov's avatar

@mattb no, the sorting is fine, but the grouping is not. And it will not work that way.

MattB's avatar
Level 2

Right so how should it be chained please?

shez1983's avatar

in your group by clause you need to put all other columns as well...

so groupBy('company', 'col1', 'col2') its a SQL 'problem' not a laravel one.

MattB's avatar
Level 2

Why is this? Would this not cause confusion with grouping by multiple columns? How would this help the sort order?

Nakov's avatar

@mattb it is basically your problem. If you expect to get Categories all grouped by the Company name, you won't. So you either need to specify that you don't care which will mean select just the company from the table and group by it, or if you group by multiple columns it will be the same as if not using the Grouping at all.

So I don't even understand why would you write the company name as a text in the Category and not have a dedicated table for the companies and then use a foreign key, so it will be much easier to group categories based on their company instead of the company name.

Tray2's avatar

Group by usually requires a aggregate like sum() or avg() something like this query

SELECT sum(in_stock) stock, some_field2, some_field3
FROM some_table
GROUP BY some_field2, some_field3
ORDER BY some_field1

Not knowing your data or what you expect as the result it's hard to help. Rule of thumb don't use group by unless you use an aggregate that requires it.

In your case from the qurey you've written you might be looking to sort on more than one column.

Category::orderBy('company')->orderBy('category')->get();

Please or to participate in this conversation.