group by stations in Laravel

*爱你&永不变心* 提交于 2019-12-25 09:48:12

问题


I'm trying to display the results of this query in laravel, this time only using fluent since Eloquent results where too nested, there're are data retrived from 6 tables in total

$result = \DB::table('estacion')

          ->join('equipo_estacion as eq_est', 'estacion.id','=', 'eq_est.estacion_id')
          ->join('equipo as eq', 'eq_est.equipo_id', 'eq.id')
          ->join('equipo_parametro as eq_param','eq.id','eq_param.equipo_id')
          ->join('parametro as pa','eq_param.parametro_id','pa.id')
          ->join('region','estacion.region_id','region.id')
          ->select('estacion.id','estacion.nombre as nom_est','eq.id as id_eq','eq.nombre as nombre_eq','pa.nombre as nombre_parametro','region.nombre as nombre_region')
          ->get();

I got this collection as a result, and i need to display data in this format shown in the two images below:

collection retrieved

i just need one row per station (ex. Antofagasta) and i get the data in two separated arrays because Antofagasta has two equipments, each station can have 0, 1 or many equipments (Equipo), so if station Antofagasta has two equipments(Environment 101m and Equipo3) , each equipment has 1 parameter to gauge(Environment measure MP10, Equipo3 measure NO2) , I must show every station and to the right the parameters measured by the equipments in that station, for example in the Antofagasta Station there are two equiments, I show that one measure MP10 in the respective column, and the other NO2 in the respective column.the same for the 2 other rows

   
(Title)Region   Station       MP10     MP2.5-     SO2 -    N02  -   CO   
---------------------------------------------------------------------------
(row1)Region1  Antofagasta    YES        NO       NO       YES      NO 

回答1:


Did you think about rebuild query? you can use pivot in SQL (depends on RDBMS) to build columns with you equipments.

Sorry that in answer not comment, but I can't add comment.



来源:https://stackoverflow.com/questions/43791227/group-by-stations-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!