Laravel Orderby not working inside Eloquent whereHas relationship?

岁酱吖の 提交于 2019-12-08 02:15:02

问题


Please have look on below code.

 $serviceNew = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date')
            ->where("service_id", '1')->where('service_provider_id', $service_privider_id)->whereMonth('service_date', $month)->whereYear('service_date', $year)
            ->with('account')
            ->whereHas('account', function($qs) {
                $qs->orderBy('restaurant_name', 'DESC');
            })
            ->get();

I have multiple records in "CompletedService" and there is one parent id account_id which in account table. and i made with on account.

Already ASC and DESC tried.

Try to order by in whereHas but it's not affect on any records. Below is model relationship.

 public function account() {
    return $this->hasOne('App\Account', 'id', 'account_id');
}

i don't need to orderby in model because i used this relation in multiple time and need only order by in this single query.

And one completed service records relations have only one account. So basically i need to sort completed service records on the account field.

Output

Tables

CompletedService

|id| account_id|service_id|service_provider_id|gallons_collected|service_date
|1 | 2         | 1        | 9                 | 50              | 2017-08-29

Accounts

| id | restaurant_name|

Every help will be appreciated. Thanks in advance.!


回答1:


You need to use alternate solution for this. First you need to fetch your data/collection and then need to apply order by.

Laravel provides some functions, using that you can sort your collection.

https://laravel.com/docs/5.6/collections#method-sortby

Use sortByDesc (for descending). So here is your code :

$serviceNew = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date')
            ->where("service_id", '1')->where('service_provider_id', $service_privider_id)->whereMonth('service_date', $month)->whereYear('service_date', $year)
            ->with('account')
            ->whereHas('account')
            ->get()
            ->sortByDesc('account.restaurant_name');



回答2:


orderBy() has no effect in a whereHas() query; you need to move the orderBy() logic to a with() statement:

$serviceNew = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date')
  ->where("service_id", '1')
  ->where('service_provider_id', $service_privider_id)
  ->whereMonth('service_date', $month)
  ->whereYear('service_date', $year)
  ->with(['account' => function($qs){
    $qs->orderBy('restaurant_name', 'DESC');
  })->has('account')->get();

Edit: The above is sorting each CompletedService's Account record by restaurant_name, which doesn't accomplish anything. To sort all CompletedService models by their Account's restaurant_name column, you'll need a join() clause:

$newService = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date')
->where('service_id', '1')
->where('service_provider_id', $service_privider_id)
->whereMonth('service_date', $month)
->whereYear('service_date', $year)
->join('accounts', 'accounts.id', '=', 'completed_services.account_id')
->orderBy('accounts.restaurant_name');

You shouldn't need the with() clause anymore, but using the has() ensures you don't get null values if there's no relationship.




回答3:


whereHas() used to limit your results based on the existence of a relationship.

See : https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence

Use the below code to add consraint in relationship :

$serviceNew = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date')
        ->where("service_id", '1')->where('service_provider_id', $service_privider_id)->whereMonth('service_date', $month)->whereYear('service_date', $year)
        ->with(['account' => function($qs) {
            $qs->orderBy('restaurant_name', 'DESC'); 
        }])->get();

See : https://laravel.com/docs/5.4/eloquent-relationships#constraining-eager-loads



来源:https://stackoverflow.com/questions/45940755/laravel-orderby-not-working-inside-eloquent-wherehas-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!