Laravel 在同一个 table 的多个循环中列出分数并求和
Laravel listing and summing scores in multiple loops in the same table
我需要用一个嵌套循环在单个 table 中转换数据,首先列出国家,然后收集每个服务的点数以对城市和城市的服务进行分组。
我的 Table:服务
CREATE TABLE `Service` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`country` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`services` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`point` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `items` (`id`, `country`, `city`, `services`, `point`, `status`) VALUES
('8', 'ABD', 'TEXAS', 'Food', '20', 'active'),
('9', 'ABD', 'TEXAS', 'Food', '40', 'active'),
('10', 'ABD', 'MONTANA', 'Hotel', '10', 'active'),
('11', 'ABD', 'MONTANA', 'Food', '70', 'active'),
('12', 'ABD', 'MONTANA', 'Hotel', '20', 'active'),
('13', 'ABD', 'TEXAS', 'Shopping', '80', 'active'),
('14', 'ITALY', 'ROMA', 'Shopping', '20', 'active'),
('15', 'ITALY', 'ROMA', 'Hotel', '90', 'active'),
('16', 'ITALY', 'LAZIO', 'Shopping', '80', 'active'),
('17', 'TURKEY', 'ANTALYA', 'Hotel', '10', 'active'),
('18', 'TURKEY', 'ISTANBUL', 'Shopping', '100', 'active'),
('19', 'TURKEY', 'ISTANBUL', 'Hotel', '50', 'active'),
('20', 'TURKEY', 'ISTANBUL', 'Hotel', '90', 'active');
我想要得到的结果
在此先感谢您的帮助
您可以使用集合对国家和城市进行分组
$service= \App\Models\Service::get()->groupBy(['country','city']);
已更新
$service= \App\Models\Service::get()->groupBy(['country','city','services'])->map(function ($city) {
return $city->map(function ($services) {
return $services->map(function ($service) {
return $service->sum('point');
});
});
});
已更新
$service= \App\Models\Service::get()->groupBy(['country','city','services'])->map(function ($city) {
return $city->map(function ($services) {
return $services->map(function ($service) {
return ['serviceName'=>$service->first()->services,'sum'=>$service->sum('point'),"count"=>$service->count()];
});
});
});
我需要用一个嵌套循环在单个 table 中转换数据,首先列出国家,然后收集每个服务的点数以对城市和城市的服务进行分组。
我的 Table:服务
CREATE TABLE `Service` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`country` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`services` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`point` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `items` (`id`, `country`, `city`, `services`, `point`, `status`) VALUES
('8', 'ABD', 'TEXAS', 'Food', '20', 'active'),
('9', 'ABD', 'TEXAS', 'Food', '40', 'active'),
('10', 'ABD', 'MONTANA', 'Hotel', '10', 'active'),
('11', 'ABD', 'MONTANA', 'Food', '70', 'active'),
('12', 'ABD', 'MONTANA', 'Hotel', '20', 'active'),
('13', 'ABD', 'TEXAS', 'Shopping', '80', 'active'),
('14', 'ITALY', 'ROMA', 'Shopping', '20', 'active'),
('15', 'ITALY', 'ROMA', 'Hotel', '90', 'active'),
('16', 'ITALY', 'LAZIO', 'Shopping', '80', 'active'),
('17', 'TURKEY', 'ANTALYA', 'Hotel', '10', 'active'),
('18', 'TURKEY', 'ISTANBUL', 'Shopping', '100', 'active'),
('19', 'TURKEY', 'ISTANBUL', 'Hotel', '50', 'active'),
('20', 'TURKEY', 'ISTANBUL', 'Hotel', '90', 'active');
我想要得到的结果
在此先感谢您的帮助
您可以使用集合对国家和城市进行分组
$service= \App\Models\Service::get()->groupBy(['country','city']);
已更新
$service= \App\Models\Service::get()->groupBy(['country','city','services'])->map(function ($city) {
return $city->map(function ($services) {
return $services->map(function ($service) {
return $service->sum('point');
});
});
});
已更新
$service= \App\Models\Service::get()->groupBy(['country','city','services'])->map(function ($city) {
return $city->map(function ($services) {
return $services->map(function ($service) {
return ['serviceName'=>$service->first()->services,'sum'=>$service->sum('point'),"count"=>$service->count()];
});
});
});