How do I export an excel in laravel with stored procedure?

0

I am trying to export an excel with a stored procedure that I have created, for this I sent the parameters and everything is fine, it returns a complete array, but there are some null values.

Then I do the following (I execute the query with static function)

 $datos_mmu = S_Reporte_MMU_Cedente::get_reporte_mmu_cedente($id_cedente,$id_cartera,$tipo_mmu,$tipo_reporte);

Then I pass the data with use in Maatwebsite / excel (I have exported the excel facade), and I do it this way:

return Excel::create('Reporte Mmu', function($excel) use ($datos_mmu){
    $excel->sheet('Excel sheet', function($sheet) use ($datos_mmu) {
        $sheet->fromArray($datos_mmu);
    });
})->export('xls');

But when I try to download it gives me the error

  

Object of class stdClass could not be converted to string

Then I do not know if I get that error because I have null values or because it is done differently. The array has to be dynamic since I do all this with stored procedures and the columns can change.

I tried to do it in the following way but it does not work.

$array_data = [];
        for ($c = 0;$c<sizeof($datos_mmu);$c++)
        {
            foreach ($datos_mmu[$c] as $i=>$cedente)
            {
                $array[$i] = (array)$cedente;
            }

            array_merge($array_data,$array);
        }
    
asked by Sebastián Lagos Yañez 31.05.2018 в 17:33
source

1 answer

2

Well, it turns out that after so much searching it concludes that it was done in the following way dynamically

 $array_data = [];

        //formar array con llave de nombre de columnas para exportar
        foreach((array)$datos_mmu as $datos){
            foreach((array)$datos as $llave => $valor){
                $columnas_excel[$llave]=$valor;
            }
            array_push($array_data,$columnas_excel);
        }

First I declare a% co_of% empty and then do $array_data I have to enter the array.

Then with two foreach

The first is that I take the data data that has the keys and the data, Example:

array:1039 [▼
  0 => {#1053 ▼
    +"NombreColumna1": 000000
    +"NombreColumna2": "valor"
    +"NombreColumna3": "valor"
  }
  1 => {#1054 ▶}

Something like that, then with the second foreach I go through it internally one by one to form the array and keep it that way

array:1039 [▼
  0 => array:25 [▼
    "NombreColumna1": 000000
    "NombreColumna2": "valor"
    "NombreColumna3": "valor"
  ]
  1 => array:25 [▶]

And once formed, I can pass the variable that forms what would be push and will download the file. I hope you greet them.

    
answered by 31.05.2018 / 22:41
source