Answer the question
In order to leave comments, you need to log in
How to sort items by related table field in Laravel?
There are `Product` and `Brand` models. And the `Brand` model has `position` properties.
How to get sorted elements by `position` field of `Brand` model?
Tried like this:
$items = Product::with([
'brand' => function ($query) {
$query->orderBy('position', 'ASC');
}
])
->paginate(10);
$items = Product::join('brands', 'product.brand_id', '=', 'brands.id')
->orderBy('brands.position', 'ASC')
->paginate(10);
Answer the question
In order to leave comments, you need to log in
А подобные решения для Yii2 есть?
Весь гугл перегуглил..
Весь yandex перерыл..
Ни где не могу найти решения проблемы.
Вопрос точ в точ такой же - "Как сортировать элементы по полю связанной таблицы" только в Yii2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question