Laravel 7 leftJoin、Distinct 和按相关排序 created_at
Laravel 7 leftJoin, Distinct and Sorted by related created_at
我正在开发一个列表广告网站,专门用于 Laravel 7 的二手视频游戏。我有一个 'GAMES' table 和一个 'LISTINGS' table。
游戏
CREATE TABLE `games` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `fulltext_index` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=10230 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
列表
CREATE TABLE `listings` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`game_id` bigint(20) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `listings_user_id_foreign` (`user_id`),
KEY `listings_game_id_foreign` (`game_id`),
CONSTRAINT `listings_game_id_foreign` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`) ON DELETE CASCADE,
CONSTRAINT `listings_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=412 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
我需要所有 'GAMES'(不是列表)的分页列表,这些列表按最新 'LISTING' 的 'created_at' 列排序。
感谢@mayankmodi 帮助解决了其中一个问题,我更新了以下部分以专注于排序问题。
如果我在游戏控制器中这样做:
$games = Game::leftJoin('listings', function($leftJoin)
{
$leftJoin->on('listings.game_id', 'games.id')
->whereNull('listings.deleted_at');
})
->select('games.*', 'listings.created_at')
->orderByDesc('listings.created_at')
->groupBy('games.id')
->with(['listings' => function ($query) {
$query->latest();
}])
->simplePaginate(36);
我的游戏是不同的,但不是按上次附加的顺序排列的 listing.created_at。
你知道如何解决这个问题吗?
好的,我让它按预期工作了。
如果有人可以从 4 个多小时的尝试中获益,我会在此处粘贴我的解决方案。
$games = Game::leftJoin('listings', function($leftJoin){
$leftJoin->whereNull('listings.deleted_at')
->on('listings.game_id', 'games.id');
})
->select('games.*', DB::raw('MAX(listings.id) AS latest_listing'))
->groupBy('games.id')
->orderByDesc('latest_listing')
->with('listings')
->simplePaginate(36);
感谢您的帮助!
我正在开发一个列表广告网站,专门用于 Laravel 7 的二手视频游戏。我有一个 'GAMES' table 和一个 'LISTINGS' table。
游戏
CREATE TABLE `games` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `fulltext_index` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=10230 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
列表
CREATE TABLE `listings` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`game_id` bigint(20) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `listings_user_id_foreign` (`user_id`),
KEY `listings_game_id_foreign` (`game_id`),
CONSTRAINT `listings_game_id_foreign` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`) ON DELETE CASCADE,
CONSTRAINT `listings_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=412 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
我需要所有 'GAMES'(不是列表)的分页列表,这些列表按最新 'LISTING' 的 'created_at' 列排序。
感谢@mayankmodi 帮助解决了其中一个问题,我更新了以下部分以专注于排序问题。
如果我在游戏控制器中这样做:
$games = Game::leftJoin('listings', function($leftJoin)
{
$leftJoin->on('listings.game_id', 'games.id')
->whereNull('listings.deleted_at');
})
->select('games.*', 'listings.created_at')
->orderByDesc('listings.created_at')
->groupBy('games.id')
->with(['listings' => function ($query) {
$query->latest();
}])
->simplePaginate(36);
我的游戏是不同的,但不是按上次附加的顺序排列的 listing.created_at。
你知道如何解决这个问题吗?
好的,我让它按预期工作了。
如果有人可以从 4 个多小时的尝试中获益,我会在此处粘贴我的解决方案。
$games = Game::leftJoin('listings', function($leftJoin){
$leftJoin->whereNull('listings.deleted_at')
->on('listings.game_id', 'games.id');
})
->select('games.*', DB::raw('MAX(listings.id) AS latest_listing'))
->groupBy('games.id')
->orderByDesc('latest_listing')
->with('listings')
->simplePaginate(36);
感谢您的帮助!