Support the ongoing development of Laravel.io →
Database Eloquent

Currently I am retrieving product results for a category like this:

$products = $category->products()->with('prices')->paginate(12);

A category is related to a product via a 'category_product' table and the product's prices are stored in a 'prices' table (as each product might have more than one price associated with it.

I would like to sort the results by price (the prices() relation is sorted this way on the model).

Is it possible to do this? I have tried ->orderBy('prices.amount') and variants. Can't get my head around it.

Thank you.

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

skinnyvin skinnyvin Joined 18 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.