I
I
Igor Koch2017-10-18 13:58:54
MySQL
Igor Koch, 2017-10-18 13:58:54

How to do orderBy on related table?

There are 2 tables:
- Products (HasMany Variants)
- Product Variants (linked via variant.product_id)

$products = Product::where('products.visible', 1)
  ->whereHas('variants', function ($query) {
    $query->where('visible', 1);
  })->orderBy('id', 'asc')
  ->paginate($products_per_page);

How to do orderBy on related table?
You need to sort the products by the lowest price option

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ellrion, 2017-10-19
@ellrion

https://gist.github.com/Ellrion/5d873fc8acf4d18464...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question