12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace App\Exports;
- use Maatwebsite\Excel\Concerns\FromCollection;
- class TraceExport implements FromCollection
- {
- private $row;
- private $data;
- public function __construct($row,$data)
- {
- $this->row = $row;
- $this->data = $data;
- }
- public function collection()
- {
- $row = $this->row;
- $data = $this->data;
- //设置表头
- $key_arr=[];
- foreach ($row[0] as $key => $value) {
- $key_arr[] = $key;
- }
- //输入数据
- foreach ($data as $keys => $value) {
- $js = [];
- for ($i=0; $i < count($key_arr); $i++) {
- if($key_arr[$i]=='code'){
- $js = array_merge($js, [$key_arr[$i] => env('APP_URL').(string)$value[$key_arr[$i]]]);
- }else{
- $js = array_merge($js, [$key_arr[$i] => (string)$value[$key_arr[$i]]]);
- }
- }
- $row[]=$js;
- if($value['get_child']){
- foreach($value['get_child'] as $key=>$val){
- $js = [];
- for ($j=0; $j < count($key_arr); $j++) {
- if($key_arr[$j]=='code'){
- $js = array_merge($js, [$key_arr[$j] => env('APP_URL').(string)$val[$key_arr[$j]]]);
- }else{
- $js = array_merge($js, [$key_arr[$j] => (string)$val[$key_arr[$j]]]);
- }
- }
- $row[]=$js;
- if($val['get_child']){
- foreach($val['get_child'] as $k=>$v){
- $js=[];
- for ($z=0; $z < count($key_arr); $z++) {
- if($key_arr[$z]=='code'){
- $js = array_merge($js, [$key_arr[$z] => env('APP_URL').(string)$v[$key_arr[$z]]]);
- }else{
- $js = array_merge($js, [$key_arr[$z] => (string)$v[$key_arr[$z]]]);
- }
- }
- $row[]=$js;
- }
- }
- }
- }
- }
- return collect($row);
- }
- }
|