如何解决Laravel Morph过滤器
// 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 |
+ -- + ------------ + -------------- +
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();
问题:使用Laravel雄辩的词法关系,如何按具有morphMany多态关系的列对查询进行排序?在上面的代码中,我将检索所有事件的详细信息以及评分详细信息,并按 rate 得分排序。我如何按排序更高的出价以及总汇率表格行(大多数情况下是人们投票),是否表示该顺序事件的得分将基于更高的分数,大多数人会对该事件评价。
解决方法
您可以在此处尝试一些代码:
通过具有morphMany多态关系的列排序查询?
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,$month)
->orderBy('SUM(rating.score)','desc')
->get()
->toArray();
在此处了解更多信息:Laravel order results by column on polymorphic table和OrderBy Sum in Laravel
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。