- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 Laravel 的 Eloquent 模型;如何按具有 morphMany
多态关系的列对查询进行排序?
假设我有给定的表结构:
// table: images
+ -- + ------------ + -------------- + ----- + ----------- +
| id | imageable_id | imageable_type | views | upload_date |
+ -- + ------------ + -------------- + ----- + ----------- +
| 1 | 1 | User | 100 | 2016-16-06 |
| 2 | 3 | User | 200 | 2016-16-06 |
+ -- + ------------ + -------------- + ----- + ----------- +
// table: users
+ -- + ---- +
| id | name |
+ -- + ---- +
| 1 | Bob |
| 2 | Kat |
+ -- + ---- +
...我有一个 Eloquent User
模型,它具有 morphMany
多态关系。
我如何根据用户今天上传的图片获得的浏览量对用户进行排序?
我自己已经尝试过这个问题,但未能找到解决方案。我最接近正确排序的是当我使用与此类似的查询时:
User::with([
'images' => function ($query) {
$query->where('upload_date', Carbon::today()->toDateString())
->orderBy('views', 'DESC');
}
])->get();
但是,在尝试这个之后,我很明显地发现用户现在按他们递增的 ID 错误排序,而不是 images
表上的 views
列。
提前感谢在我解决这个问题的过程中帮助我的任何人。
最佳答案
感谢这篇文章的评论,我明白了 http://laravel.io/forum/02-21-2014-order-and-paginate-a-collection-by-a-releted-field-using-eloquent .
User::leftJoin('images', function ($join) {
$join->on('users.id', '=', 'images.imageable_id')
->where('imageable_type', '=', 'User')
->where('upload_date', '=', Carbon::today()->toDateString());
})
->orderBy('images.views')
->select(['players.*']);
我之前尝试过类似的方法,但遇到了问题,因为 ID 和其他列在两个连接表之间发生冲突。这次的区别是 ->select(['users.*'])
。现在,只返回用户的数据。
关于php - Laravel 按多态表上的列排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852995/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!