Laravel listing and summing scores in multiple loops in the same table

I need to turn the data in a single table with a nested loop and first list the countries and then collect the points of each service to group the cities and services of the cities.

My Table : Services

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');

Laravel listing and summing scores in multiple loops in the same table

The result I want to get
Laravel listing and summing scores in multiple loops in the same table

Thanks in advance for your help

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can group country and city using collection

$service= AppModelsService::get()->groupBy(['country','city']);

Updated

$service= AppModelsService::get()->groupBy(['country','city','services'])->map(function ($city) {

     return $city->map(function ($services) {

          return $services->map(function ($service) {

                return $service->sum('point');

            });

        });
       
    });

Updated

$service= AppModelsService::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()];

                });

            });
           
        });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x