为什么我得到一个没有数据的空Excel文件?

时间:2019-07-23 12:20:13

标签: excel laravel laravel-5 laravel-excel

以下代码从两个不同的表构建两个Excel电子表格,并将文件存储在本地目录中:

<?php

namespace App\Http\Controllers;

use App\Event;
use App\Visitor;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use \Carbon\Carbon;

class VisitorsSheet implements FromQuery, WithTitle
{

    private $idEvent;

    public function __construct($idEvent)
    {

        $this->idEvent = $idEvent;
    }

    public function query()
    {
        return Visitor::query()->where("idEvent", $this->idEvent)->get();
    }

    public function title(): string
    {
        return 'Visitors';
    }
}

class EventSheet implements FromQuery, WithTitle
{
    private $idEvent;

    public function __construct($idEvent)
    {

        $this->idEvent = $idEvent;
    }

    public function query()
    {
        return Event::query()->where("idEvent", $this->idEvent)->get();
    }

    public function title(): string
    {
        return 'Event №' . $this->idEvent;
    }
}

class ArchivingExport implements WithMultipleSheets
{

        use Exportable;

    private $eventId;

    public function __construct($eventId)
    {

        $this->eventId = $eventId;
    }

    public function sheets(): array
    {
        $sheets = [];
        $sheets[] = new EventSheet($this->eventId);
        $sheets[] = new VisitorsSheet($this->eventId);

        return $sheets;
    }
}

class ArchivingController extends Controller
{


    private $file = "settings_archive.json";

    public function __construct()
    {
        date_default_timezone_set("Asia/Baku");
    }

    private function formatName($event)
    {

        $date = Carbon::createFromFormat('Y-m-d h:m:s', $event->date)->format('d-m-Y');

        return $date . '_' . $event->name . '.xlsx';
    }

    public function index()
    {



            try {
                $events = Event::where("status", 1)->where("archived", 0)->get();
                foreach ($events as $key => $event) {

                    Excel::store(new ArchivingExport($event->idEvent), 'archive/' . $this->formatName($event));

                    Event::where("idEvent", $event->idEvent)->update(["archived" => 1]);
                }


                return \json_encode([
                    'status' => true,
                    'message' =>  'Handled: '.$events->count()
                ]);

            } catch (Exception $e) {
                 return \json_encode([
                    'status' => false,
                    'message' =>  $e->getMessage()
                ]);

            }

       // }

        return \json_decode([
            'status' => false
        ]);

    }


    }

}

预期的输出:创建两个内部包含数据的电子表格

当前输出:创建两个电子表格,但内部没有任何数据。

出什么问题了?我敢肯定,数据库中有数据,查询返回数据。

初始类别为:class ArchivingController extends Controller {}

我已经使用过this教程

1 个答案:

答案 0 :(得分:1)

我不是PHP开发人员从未使用过该语言,但是您确定将数据保存到工作表中时可能会返回它,却忘记了在创建数据时将数据放入工作表中吗?

-我本来会发表评论,但代表不够多。