jeudi 2 juillet 2020

Loop through collection in laravel and get specific data from the collection

I'm trying to go through a collection and I need to get a list of products according to certain keys. but I get the following error when I print the list with dd:

"array_key_exists(): The first argument should be either a string or an integer"

this is my function

public function reemplazaCostoZona($zona, $destino, $especie, $costo, $transporCollect){

 $productos = $transporCollect->where([['zona',$zona],['destino', $destino],['especie',$especie]])
                                     ->pluck('producto');
 dd($productos );

}

Collection:

enter image description here

I need to obtain the list of products for destination zone and species, That is to say that for my example of collection, for zone 1 destination 5 specie 1 I would have in my list product 1 and there may be more.

function where Im obtain collection:

public function storeTranspor3($request){
      DB::table('transpor')->truncate();
      $var = DB::select("select flu.zona, flu.destino, flu.producto, pro.especie, sup.codigo, dtr.cod_fundo, sup.sup_ha, dtr.dist_pavimento, dtr.dist_no_pavimento, dtr.peaje 
      from flujos flu 
      left join super sup on (sup.zona = flu.zona) 
      left join d_transporte dtr on (dtr.cod_fundo = (sup.codigo / 1000000) and dtr.destino = flu.destino) 
      left join productos pro on (pro.producto = flu.producto) 
      left join especies esp on (esp.especie = pro.especie)
      order by flu.zona, dtr.cod_fundo, flu.producto, flu.destino, sup.codigo");
      $tansporCollect = collect();
      $tansporCollectsinCosto = collect();
      foreach ($var as $f) {
        if($f->codigo != null){    
          if($f->especie == 1){      
            $a = 0.177548*$f->dist_no_pavimento;
            $b = 0.0746*$f->dist_pavimento;
            $c = 0.0333*$f->peaje;
            $costoFundo = ($a + $b + 1.1191 + 0.399 + $c)*$f->sup_ha;
          }
          else{
            $a = 0.1652*$f->dist_no_pavimento;
            $b = 0.0694*$f->dist_pavimento;
            $c = 0.0357*$f->peaje;
            $costoFundo = ($a + $b + 1.0421 + 0.599 + $c)*$f->sup_ha;
          }   
            $tansporC =[
              'zona' => $f->zona,
              'destino' => $f->destino,
              'producto' => $f->producto,
              'especie' => $f->especie,
              'costo' => $costoFundo
            ];
            $tansporCollect->push($tansporC);

            $tansporsinCosto = [
              'zona' => $f->zona,
              'destino' => $f->destino,
              'producto' => $f->producto,
              'especie' => $f->especie,
            ];
            $tansporCollectsinCosto->push($tansporsinCosto);
        }
      }
      
      $zonas = $tansporCollect->pluck('zona')->unique();
      $destinos = $tansporCollect->pluck('destino')->unique();
      $especies = $tansporCollect->pluck('especie')->unique();
      $transporCollectUnique = $tansporCollectsinCosto->unique();
      
      foreach($zonas as $zon){
        $costoZona = $tansporCollect->where('zona',$zon);
        foreach($destinos as $destin){
          $costoDestino = $costoZona->where('destino',$destin);
          foreach($especies as $espec){
            $costoEspecie = $costoDestino->where('especie',$espec)->avg('costo');
            /*HERE IM CALL OTHER FUNCTION */
            $this->reemplazaCostoZona($zon, $destin, $espec, $costoEspecie, $transporCollectUnique);
        
          }
        }
      }  
  }

I'm with laravel 5.8



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire