【问题标题】:Laravel generate excel from query returns an error using maatwebsiteLaravel 从查询中生成 excel 使用 maatwebsite 返回错误
【发布时间】:2020-01-30 17:03:12
【问题描述】:

基本上我得到了一个查询,结果将被创建为一个 excel 文件。这是我的代码

$data = \DB::table('checkers')
        ->where('remarks_id',2)
        ->join('schedules','schedules.id','=','checkers.schedule_id')
        ->join('teachers','schedules.teacher_id','=','teachers.id')
        ->join('subject_codes','subject_codes.id','=','schedules.subject_code_id')
        ->join('remarks','remarks.id','=','checkers.remarks_id')
        ->where('checkers.created_at', '=>', $request->from)
        ->where('checkers.created_at', '=<', $request->to)
        ->select('teachers.fullname','subject_codes.subject_description','remarks.remarks_desc','checkers.created_at')
        ->get(); 

        $data = $data->map(function ($x) {
            return json_decode(json_encode($x), true);
        })->all();

        // dd($data);

        Excel::create('report', function($excel) use($data) {
            $excel->sheet('Sheetname', function($sheet) use($data) {
            });
        })->export('xls');

当我执行`dd($data)

array:2 [
  0 => {#294
    +"fullname": "Edcel Pagagao"
    +"subject_description": "Capstone 1"
    +"remarks_desc": "No Teacher Around"
    +"created_at": "2020-01-30 00:00:00"
  }
  1 => {#298
    +"fullname": "Edcel Pagagao"
    +"subject_description": "Capstone 1"
    +"remarks_desc": "No Teacher Around"
    +"created_at": "2020-01-27 00:00:00"
  }
]

这就是文档所说的 https://docs.laravel-excel.com/2.1/export/array.html 基于 2.x 版本

但我仍然收到错误continue" targeting switch is equivalent to "break". Did you mean to use "continue 2

【问题讨论】:

  • -&gt;sheet(... 里面有代码吗?
  • 你的意思先生@FelippeDuarte
  • 我的意思是,如果这是您的真实代码,或者您实际上将$data 放入工作表中的位置是否有更多代码。
  • 我正在使用use($data)
  • 你为什么要做这部分return json_decode(json_encode($x), true);

标签: php excel laravel maatwebsite-excel


【解决方案1】:

根据您的链接 Link 第一个示例说您需要传递 array 数据,如下所示

$array_data = array(
    array('data1', 'data2'),
    array('data3', 'data4')
 );
$sheet->fromArray($array_data);

您可以使用 toArray()

将集合更改为数组
$data = \DB::table('checkers').....->select('teachers.fullname','subject_codes.subject_description',
'remarks.remarks_desc','checkers.created_at')
->get()
->toArray();

【讨论】:

  • 我试过了,先生,但它说Object of class stdClass could not be converted to string"
  • 你能在解码或编码之前给我看dd($data)吗??
  • 先生,我们可以在这里讨论吗?我真的需要帮助join.slack.com/t/vue-vmr4432/shared_invite/…
猜你喜欢
  • 1970-01-01
  • 2019-11-06
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多