Mysql 查询的特定数据结果

Mysql 查询的特定数据结果,mysql,laravel,fluent,Mysql,Laravel,Fluent,我无法按estacion.id对sql查询进行分组 $result = \DB::table('estacion') ->select('estacion.id as id_est','estacion.nombre as nombre_estacion','eq.id as id_equipo', 'eq.nombre as nombre_equipo','pa.nombre as nombre_parametro')

我无法按estacion.id对sql查询进行分组

$result = \DB::table('estacion')
           ->select('estacion.id as id_est','estacion.nombre as nombre_estacion','eq.id as id_equipo',
             'eq.nombre as nombre_equipo','pa.nombre as nombre_parametro') 
           ->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')
           ->get();
当在图像上重复设置时,我想要的结果 ,这样我就可以在嵌套数组中循环

迁移

   public function up()
  {
    Schema::create('estacion', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->integer('xcoord');
        $table->integer('ycoord');
        $table->date('inicio_actividad')->nullable();
        $table->date('fin_actividad')->nullable();
        $table->string('url');
        $table->integer('comuna_id')->unsigned()->nullable();
        $table->foreign('comuna_id')->references('id')->on('comuna');
        $table->integer('region_id')->unsigned()->nullable();
        $table->foreign('region_id')->references('id')->on('region');

        $table->timestamps();
    });
}

   public function up()
{
    Schema::create('equipo_estacion', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('equipo_id')->unsigned();
        $table->foreign('equipo_id')->references('id')->on('equipo')->onDelete('cascade');
        $table->integer('estacion_id')->unsigned();
        $table->foreign('estacion_id')->references('id')->on('estacion')->onDelete('cascade');
        $table->timestamps();
    });
}

 public function up()
{
    Schema::create('equipo', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->text('descripcion')->nullable();
        $table->string('marca');
        $table->string('modelo');
        $table->string('nserie')->nullable();
        $table->timestamps();
    });
}


public function up()
{
    Schema::create('equipo_parametro', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('equipo_id')->unsigned();
        $table->foreign('equipo_id')->references('id')->on('equipo')->onDelete('cascade');
        $table->integer('parametro_id')->unsigned();
        $table->foreign('parametro_id')->references('id')->on('parametro')->onDelete('cascade');
        $table->timestamps();
    });
}

  public function up()
{
    Schema::create('parametro', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->timestamps();
    });
}
预期表格结果

  (Title)Region   Station       MP10     MP2.5-     SO2 -    N02  -   CO   
  ----------------------------------------------------------------------
(row1)Region1  Antofagasta    YES        NO       NO       YES      NO 
  Region 2     Arica           No       yes       no       no       no


#table: "estacion"
  +sortable: array:8 [▶]
  #fillable: array:5 [▶]
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:14 [▶]
  #original: array:14 [▼
    "id" => 7
    "nombre" => "Antofagasta"
    "xcoord" => 1414
    "ycoord" => 1717
    "inicio_actividad" => null
    "fin_actividad" => null
    "url" => "1493682540.jpg"
    "comuna_id" => 12
    "region_id" => 3
    "red_id" => 1
    "huso_id" => 1
    "datum_id" => 2
    "created_at" => "2017-05-01 23:47:41"
    "updated_at" => "2017-05-01 23:49:00"
  ]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: array:1 [▼
    "equipos" => Collection {#363 ▼
      #items: array:2 [▼
        0 => Equipo {#373 ▼
          #table: "equipo"
          +sortable: array:7 [▶]
          #fillable: array:17 [▶]
          #connection: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:19 [▶]
          #original: array:23 [▶]
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #events: []
          #observables: []
          #relations: array:2 [▼
            "pivot" => Pivot {#377 ▶}
            "parametros" => Collection {#385 ▶}
          ]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
        1 => Equipo {#382 ▼
          #table: "equipo"
          +sortable: array:7 [▶]
          #fillable: array:17 [▶]
          #connection: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:19 [▶]
          #original: array:23 [▶]
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #events: []
          #observables: []
          #relations: array:2 [▼
            "pivot" => Pivot {#376 ▶}
            "parametros" => Collection {#386 ▶}
          ]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
      ]
    }
视图中的循环(这与我预期的非常接近,但如果一个站点有多个参数,则第二个、第三个etc参数与列名不匹配

  @foreach($result as $estacion)
           <tr> 
            <td> region </td>
            <td>{{ $item1->nombre  }} </td>

           @foreach($estacion->equipos as $equipo)

            @php($i = 0) 
             @foreach($equipo->parametros as $param)


              <!--Aca comprueba en que posicion dentro del arreglo esta el parametro -->
              @php ($posicion = array_search($param->nombre, $param_header))
                 {{ 'Las posicion es : ->' .$posicion }}</br> 

                  @while ($i < 7 )
                     {{  'valor de i' . $i}} </br>
                    @if($i == $posicion)

                        <td>{{ $param->nombre }} </td>

                          {{ Session::put('break', $i) }}
                           @break
                      @else
                          <td> no </td>
                    @endif   

                  @php ( $i++ )     

                  @endwhile 
                 <!--  @php ( $i++ )    -->

                 {{ Session::flush() }}   
              @endforeach

           @endforeach
          </tr>  
         @endforeach 
@foreach($result as$estacion)
区域
{{$item1->nombre}
@foreach($estacion->equipos as$equipo)
@php($i=0)
@foreach($equipo->parametros作为$param)
@php($posicion=array\u search($param->nombre,$param\u header))
{{'Las posicion es:->'.$posicion}}
@而($i<7) {{'valor de i.$i}
@如果($i==$posicion) {{$param->nombre} {{Session::put('break',$i)} @中断 @否则 不 @恩迪夫 @php($i++) @结束时 {{Session::flush()}} @endforeach @endforeach @endforeach

当我使用eloquent时得到的结果图像,请注意,第2位是对应列右侧的1个位置。

使用eloquent Relationship

estacion

public function equipos(){
    return $this->belongsToMany('App\Equipo', 'equipo_estacion', 'estacion_id', 'equipo_id');
}
equipo

public function parametros(){
    return $this->belongsToMany('App\Parametro', 'equipo_parametro', 'equipo_id', 'parametro_id');
}
那么您的查询应该是这样的

$result = Estacion::all()->with('equipos.parametros')->get();
然后在你的回路上

foreach ($result as $estacion) {
   echo $estacion->equipos->first()->parametros->first()->nombre;
}

你应该使用雄辩的关系..我之前确实使用过雄辩,但我面临的问题是要获得与属于一个站点的设备相关的参数,这里有更多信息,你能显示数据库和预期输出吗?我编辑了迁移和上面的结果,MP10、MP2.5等是参数,我再次比较位置nst将另一个数组中的所有参数按顺序排列,因此,如果数据库检索行中有一个参数为mp10的设备,我会在相应列中显示“是”,问题是某些站点可能有许多设备,而每个设备都有一个参数,因此我只需要在列$result=Estacion::with下面的站点行中显示它('equips.parametros')->get();这是一个有效的方法,不,尝试获取非对象的属性不起作用(视图:C:\wamp\www\bitacolar\resources\views\reportes\equips\u estacion.blade.php),我在上面编辑了几乎对我有用的内容,使用Elounce,你是否使用
php artisan make:model:estacion--migration来创建你的模型和迁移?以及其他模型?是的,我编辑了,所有的关系都很好,我得到了关于收集的所有正确数据如果你看到上面的收集,有两个设备,每个都有1个pparameter,在嵌套的foreach中需要两个循环才能获得param->name来获得它们的位置,所以我如何在第一行显示它,因为它属于Antofagasta,我可以有3、4、5个参数,并且必须提到它是否存在于各自的参数列名称下面