1

I have a doubt and I don't know how to send a parameter so that the collection function of my export generates an excel with that data. I generate this query through a method of one of my controllers

The controller is GenerateNumbersController.php and the method is validateNumberBD

The query I generate is as follows:

$searchOrCreate = Phone::insertIgnore($consultArray);

if ($searchOrCreate) {
    $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
    ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
    ->where('DATE',$searchOrCreate)
    ->get();
}

That query generates a collection of data that is what I need to send to be exported automatically.

This is where my problem begins since I don't know how to send that variable to the PhonesExport class, which is the class that generates exports to Exce

I tried to create a new function in the PhonesExport class called data ($ value) as follows:

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use App\Http\Controllers;

class PhonesExport implements FromCollection
{   
    public $data;

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        global $data;       
        return $data;
    }

    public function data($dato)
    {
        global $data;
        $data = $dato;
    }
}

And through the method of the first code try to generate an instance of that class, and then send it by the same parameter as follows.

$searchOrCreate = Phone::insertIgnore($consultArray);

if ($searchOrCreate) {
    $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
    ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
    ->where('DATE',$searchOrCreate)
    ->get();    

    $excel = new PhonesExport();
    $excel->data($phones );

    \Excel::download($excel,'phones.xlsx');
}

But when executing it generates nothing to me.

1 Answer 1

2

Option 1: Use Dependency Injection.

$searchOrCreate = Phone::insertIgnore($consultArray);
if ($searchOrCreate) {
    $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
    ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
    ->where('DATE',$searchOrCreate)
    ->get();    

    $excel = new PhonesExport($phones);

    \Excel::download($excel,'phones.xlsx');
}
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;

class PhonesExport implements FromCollection
{
    protected $phones;

    public function __construct($phones)
    {
       $this->phones = $phones;
    }

    /**
     * @return Collection
     */
    public function collection()
    {
       return $this->phones;
    }
}

Option 2: Move the query to the Exporter class.

$searchOrCreate = Phone::insertIgnore($consultArray);
if ($searchOrCreate) {    
    $excel = new PhonesExport($searchOrCreate);

    \Excel::download($excel,'phones.xlsx');
}
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;
use App\Phone;

class PhonesExport implements FromCollection
{
    protected $date;

    public function __construct($date)
    {
       $this->date = $date;
    }

    /**
     * @return Collection
     */
    public function collection()
    {
       return Phone::select('PHONES.PHONE','AREACODES.CODE')
       ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
       ->where('DATE', $this->date)
       ->get();
    }
}

Option 3: Force Download with readfile.

Instead of calling \Excel::download(...), call \Excel::store(...) and then return a download to the file.

// \Excel::download($excel,'phones.xlsx');
\Excel::store($excel, 'tmp.xlsx');
$filename = 'phones.xlsx';
$filepath = storage_path('app/tmp.xlsx');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit;
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;
use App\Phone;

class PhonesExport implements FromCollection
{
    protected $date;

    public function __construct($date)
    {
       $this->date = $date;
    }

    /**
     * @return Collection
     */
    public function collection()
    {
       return Phone::select('PHONES.PHONE','AREACODES.CODE')
       ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
       ->where('DATE', $this->date)
       ->get();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

No, it doesn't download me. If I do dd if it shows me what I need and everything. But it doesn't make me download the file, only if I press a button to download.
No, he must do the automatic download while the code is running. If I finish executing the code and give the download button if it downloads but it is not what is needed.
Why? Are you trying to download through javascript or console?
I'm still not sure I understand why you want that but I'll edit another alternative
I explain a little. My app is a number generator, when it generates those numbers and inserts them into the database I automatically have to export an excel file of what was generated, currently it does not download the file automatically, if it downloads if and only if I press a button to download
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.