mardi 23 juillet 2019

Why I get empty Excel file without data?

The following code builds two Excel sheets from two different tables and stores file in local directory:

<?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
        ]);

    }


    }

}

It works, creates files with two sheets, but without any data inside.

What is problem? I'm sure, there is data in database and query returns data.

Initial class is: class ArchivingController extends Controller {}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire