我有以下数据库架构:
CREATE TABLE `languages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` char(2) COLLATE utf8_unicode_ci NOT NULL,
`latin_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`local_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `languages_code_unique` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`film_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `films_downloads_film_id_foreign` (`film_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads_langs` (
`download_id` int(10) unsigned NOT NULL,
`language_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`download_id`,`language_id`),
KEY `films_downloads_langs_language_id_foreign` (`language_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我会通过电影电影列表获得所有语言,然后像
一样使用它 foreach ($films as $film) $film->languages;
Laravel中的SQL查询和以下代码可以让我得到我想要的东西,但是 在平面阵列中。
$q = DB::table('films)
->select('languages.latin_name', 'films.id as film_id')
->join('films_downloads', 'films.id', '=', 'films_downloads.app_id')
->join('films_downloads_langs', 'films_downloads.id', '=', 'films_downloads_langs.download_id')
->join('languages', 'languages.id', '=', 'films_downloads_langs.language_id')
->whereIn('films_downloads.film_id', $films_ids)
->groupBy('languages.id' ,'films_downloads.app_id')
->get();
我会嵌套数组/集合/模型(首选),在一个查询中为多个电影在mysql中分组重复语言。 如何通过语言集合填写电影模型集合?