php - Laravel:如何排序相关模型的结果?
问题描述
我提出了以下查询生成器,并且我得到了准确的结果,除非我需要根据论坛的喜欢和线程总数对结果进行排序并根据它进行分页。
下面是没有 sortby 的查询生成器:
$limit = isset($_GET['limit']) && !empty($_GET['limit']) && is_numeric($_GET['limit']) ? (int) $_GET['limit'] : 10;
$page = isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page - 1) * $limit;
$forums = Forum::where('status', 1);
if(isset($_GET['search']) && !empty($_GET['search'])) {
$search = trim($_GET['search']);
$forums = $forums->where(function($query) use($search) {
$query->where('title', 'LIKE', '%' . $search . '%')->orWhere('content', 'LIKE', '%' . $search . '%');
});
}
if(isset($_GET['category']) && !empty($_GET['category'])) {
$forums = $forums->whereIn('category_id', explode(',', trim($_GET['category'])));
}
if(isset($_GET['sortby']) && !empty($_GET['sortby'])) {
$sortby = trim($_GET['sortby']);
if($sortby == 'newest') {
$forums = $forums->latest();
} elseif($sortby == 'oldest') {
$forums = $forums->oldest();
} elseif($sortby == 'popular') {
// Sort By: Popular | Logic: Total no of likes and threads to a forum
} else {
$forums = $forums->latest();
}
} else {
$forums = $forums->latest();
}
$forums = $forums->offset($offset)->limit($limit)->get();
我在论坛模型中定义了雄辩的关系如下:
public function threads()
{
return $this->hasMany('App\ForumThread', 'forum_id', 'id');
}
public function likes()
{
return $this->hasMany('App\ForumLike', 'forum_id', 'id');
}
论坛表架构:
CREATE TABLE `forums` (
`id` bigint(20) UNSIGNED NOT NULL,
`category_id` int(10) UNSIGNED NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci,
`image` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tags` text COLLATE utf8mb4_unicode_ci,
`status` tinyint(3) UNSIGNED NOT NULL DEFAULT '1',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
论坛喜欢表模式:
CREATE TABLE `forum_likes` (
`id` bigint(20) UNSIGNED NOT NULL,
`forum_id` bigint(20) UNSIGNED NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
论坛线程表架构如下:
CREATE TABLE `forum_threads` (
`id` bigint(20) UNSIGNED NOT NULL,
`forum_id` bigint(20) UNSIGNED NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
谢谢!
解决方案
计算相关模型
如果你想计算一个关系的结果数而不实际加载它们,你可以使用 withCount 方法,它会在你的结果模型上放置一个 {relation}_count 列。
因此,您可以使用withCount()
然后按放置的列对结果进行排序。例如对于“喜欢”关系:
} elseif ($sortby == 'popular') {
$forums = $forums->withCount('likes')->orderBy('likes_count', 'desc');
}
推荐阅读
- php - Apache 似乎没有检测到我的 .htaccess 文件
- php - 对象分页的 ECHO
- sql - 需要 regex_extract SQL 查询以返回“开头”和“结尾”某个字符的值
- node.js - out-ping nodejs包安装错误
- javascript - 如何使用 Vue 在 Vue 中创建通用函数?
- javascript - 当 setimeout == 0 时更改页面
- c# - 在实体框架中配置多对多关系时出现“歧义引用”错误
- anaconda - 为什么 conda 不删除已删除环境的软件包?
- html - 翻转卡不会在移动设备上翻转
- vue.js - 将 Vuex 与子组件一起使用的正确方法