I have a model Product with a hasOne relationship method getCompany().
public function getCompany(){
return $this->hasOne(Company::class, 'id', 'company_id');
}
I need to retrieve the Product collection sorted by getCompany.rate
My approach
$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();
Fails with :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'getCompany.rate' in 'order clause'
I've tried to sort it after collected
return $products->sortBy('getCompany.rate');
But this doesn't sort at all