Php 拉威尔变形滤波器

Php 拉威尔变形滤波器,php,mysql,laravel,eloquent,laravel-relations,Php,Mysql,Laravel,Eloquent,Laravel Relations,代码 // table: rates + -- + ------------ + -------------- + ----- + ----------- + | id | ratable_id | rateble_type | score | create_at |userid + -- + ------------ + -------------- + ----- + ----------- + | 1 | 1 | Events | 4 | 2

代码

// table: rates
+ -- + ------------ + -------------- + ----- + ----------- +
| id | ratable_id | rateble_type | score | create_at |userid
+ -- + ------------ + -------------- + ----- + ----------- +
| 1  | 1            | Events           | 4   | 2020-10-06  |3
| 2  | 1            | Events           | 4   | 2020-10-06  |2 
| 3  | 2            | Events           | 0   | 2020-10-06  |1
+ -- + ------------ + -------------- + ----- + ----------- +

// table: events
+ -- + ------------ + -------------- +
| id | name | rate  | create_at |
+ -- + ------------ + -------------- +
| 1  | eventd    | 4   | 2020-10-06  |
| 2  | evente    | 4   | 2020-10-06  |
| 3  | eventn    | 0   | 2020-10-06  |
+ -- + ------------ + -------------- +

问题:使用Laravel Eloquent变形关系,如何按多个变形关系上的列排序查询?在上面的代码中,我将检索所有带有评级详细信息的事件详细信息,并按评级分数排序。我怎样才能排序呢?orderby更高的比率以及总比率表行(大多数人在活动中投票),这是否意味着活动的顺序将基于更高的分数和大多数人对该事件的比率。

您可以在这里尝试一些代码:

按多个多态关系上的列排序查询

public function rating()
    {
        return $this->morphMany(Rate::class, 'ratable');
    }  

$data = Event::with(['user'])
          ->with('rating')
          ->whereMonth('created_at', $month)
          ->orderBy('finalrate', 'desc')
          ->take(5)
          ->get()
          ->toArray();
Orderby较高的比率和total rates表行(大多数人在事件中投票),是否意味着事件的顺序将基于较高的分数和大多数人对事件的评级:

public function rating()
    {
        return $this->morphMany(Rate::class, 'ratable');
    }  

$data = Event::with(['user'])
          ->with('rating')
          ->whereMonth('created_at', $month)
          ->orderBy('rating.score', 'desc')
          ->take(5)
          ->get()
          ->toArray();
在这里阅读更多:和

public function rating()
    {
        return $this->morphMany(Rate::class, 'ratable');
    }  

$data = Event::with(['user'])
          ->with('rating')
          ->whereMonth('created_at', $month)
          ->orderBy('SUM(rating.score)', 'desc')
          ->get()
          ->toArray();