Skip to content

Files

Latest commit

author
Панов Антон Евгеньевич
Jul 3, 2019
1596b33 · Jul 3, 2019

History

History
56 lines (42 loc) · 1.78 KB

Выполнить сортировку(упорядочивание) по связующему полю Eloquent.md

File metadata and controls

56 lines (42 loc) · 1.78 KB

Выполнить сортировку(упорядочивание) по связующему полю Eloquent

Вы могли бы сделать сортировку в whereHas

// Retrieve all products with at least one variant ordered by price

$query = Product::whereHas('variants', function ($query) use ($request) {
    if ($request->input('sort') == 'price') {
        $query->orderBy('variants.price', 'ASC');
    }
})
->with('reviews')

По сути, ваш подзапрос будет: select * from variants where product_id in (....) order by price , и это не то, что вы хотите, верно?

<?php 
// ...

$order = $request->sort;

$products = Product::whereHas('variants')->with(['reviews',  'variants' => function($query) use ($order) {
  if ($order == 'price') {
    $query->orderBy('price');
  }
}])->paginate(20);

Если вы хотите отсортировать товар + / или вариант, вам нужно использовать join.

$query = Product::select([
          'products.*',
          'variants.price',
          'variants.product_id'
        ])->join('variants', 'products.id', '=', 'variants.product_id');

if ($order === 'new') {
    $query->orderBy('products.created_at', 'DESC');
}

if ($order === 'price') {
    $query->orderBy('variants.price');
}

return $query->paginate(20);

PHP Eloquent сортировка (sort) упорядочивание (order)