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.