How to sort items by field to the related table in Laravel?

Have a model `Product` and `Brand`. And the model `Brand` has properties `position`.
How to obtain the sorted elements in the field `position` of the model `Brand`?

I tried this:
$items = Product::with([
 'brand' => function ($query) {
 $query->orderBy('position', 'ASC');
}
])
 ->paginate(10);


More like this:
$items = Product::join('brands', 'product.brand_id', '=', 'brands.id')
 ->orderBy('brands.position', 'ASC')
 ->paginate(10);
July 9th 19 at 11:04
2 answers
July 9th 19 at 11:06
Solution
July 9th 19 at 11:08
Solution

Find more questions by tags PHPMySQLLaravelSQL