I tried to sort products from shop_products
table by pinned
column from shop_products_options
table:
$products = ShopProduct::with(['options' => function ($query) {
$query->orderBy('pinned', 'desc');
}])->paginate(5);
I set relation in ShopProduct model:
public function options()
{
return $this->hasOne('ShopOptions');
}
But products aren't sorted. I get a query that only works with shop_products_options
table.
SELECT * FROM `shop_products_options` WHERE `shop_products_options`.`product_id` in ('8', '9', '10', '11', '12') ORDER BY `pinned` DESC
How to fix it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…