我有一个疑问,我不知道如何发送参数,以便导出的收集功能可以用该数据生成一个excel。 我通过其中一个控制器的方法生成此查询
控制器为GenerateNumbersController.php,方法为validateNumberBD
我生成的查询如下:
$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();
}
该查询会生成一组数据,这些数据是我需要发送以自动导出的数据。
这是我的问题开始的地方,因为我不知道如何将该变量发送到PhonesExport类,该类是生成导出到Exce的类
我试图在PhonesExport类中创建一个名为data($值)的新函数,如下所示:
<?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;
}
}
并通过第一个代码的方法尝试生成该类的实例,然后通过如下相同的参数将其发送。
$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');
}
但是执行时对我来说什么都没有。
答案 0 :(得分:1)
email
$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;
}
}
$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();
}
}
而不是调用\Excel::download(...)
,然后将下载内容返回文件。
\Excel::store(...)
// \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;