【发布时间】:2022-01-27 07:29:15
【问题描述】:
我有一个模型 Product 和一个 hasOne 关系方法 getCompany()。
public function getCompany(){
return $this->hasOne(Company::class, 'id', 'company_id');
}
我需要检索按getCompany.rate排序的Product集合
我的方法
$products = Product::with('getCompanyName', 'getCompany')
->whereHas('getCompany', function ($query) {
return $query->where('status_shop_id', '=', 2);
})
->whereHas('getCompany', function ($query) {
return $query->where('status', '=', 1);
})
->where('name', 'Like', "%$text%")
->whereStatus(1)
->whereTypeId(1)
->orderBy('rate', 'desc')
->limit(5)
->get();
失败:
SQLSTATE[42S22]:未找到列:1054 未知列 'getCompany.rate' 在“订单子句”中
我已经尝试在收集后对其进行排序
return $products->sortBy('getCompany.rate');
但这根本不排序
【问题讨论】:
-
如果您想根据另一个表的列进行排序,最好使用
joins。 -
@user3532758 你能告诉我吗?