by the way, you have to select it first(the companie name) what about
$products = Product::join('companies', 'companies.id', '=', 'products.company_id')
->orderBy('companies.name', 'asc')
->select('products.*, companies.name');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've been using the following code to pull out a list of products and their related companies:
$products = Product::join('companies', 'companies.id', '=', 'products.company_id')
->orderBy('companies.name', 'asc')
->select('products.*');
Which works OK but some products don't have a related company so were being excluded by the inner join. I therefore changed it to a leftJoin which does pull out all the products however this then seems to ignore the orderBy.
It seems to be ordering by the products rather than the company name when I use the leftJoin this way. Can't really understand why it would ignore the orderBy?
Please or to participate in this conversation.