在Laravel中按站分组 [英] group by stations in Laravel

查看:82
本文介绍了在Laravel中按站分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在laravel中显示此查询的结果,这次仅使用流利的语言,因为口才太差,结果总共从6个表中检索了数据

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:

已检索到集合

我每个站点只需要一行(例如Antofagasta),并且我得到了两个分离的数组中的数据,因为Antofagasta有两个设备,每个站点可以有0、1或许多设备(Equipo),所以如果站点Antofagasta有两个设备设备(环境101m和Equipo3),每个设备都有1个要测量的参数(环境测量MP10,Equipo3测量NO2),我必须显示每个站点,并在右边显示该站点的设备所测量的参数,例如在Antofagasta站点有两个条件,我表明在相应的列中一个度量MP10,在相应的列中另一个度量NO2.其他两行相同

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 

推荐答案

您是否考虑过重建查询?您可以在SQL中使用数据透视(取决于RDBMS)来与设备一起构建列.

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.

这篇关于在Laravel中按站分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆