Skip to content

Commit

Permalink
Enhance the excel export feature of model-grid
Browse files Browse the repository at this point in the history
  • Loading branch information
z-song committed Apr 11, 2019
1 parent 942e449 commit 03fbcf7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Grid/Exporters/AbstractExporter.php
Expand Up @@ -70,6 +70,22 @@ public function chunk(callable $callback, $count = 100)
return $this->grid->getFilter()->chunk($callback, $count);
}

/**
* @return \Illuminate\Support\Collection
*/
public function getCollection()
{
return collect($this->getData());
}

/**
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
*/
public function getQuery()
{
return $this->grid->getFilter()->getModel()->getQueryBuilder();
}

/**
* Export data with scope.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Grid/Exporters/ExcelExporter.php
@@ -0,0 +1,48 @@
<?php

namespace Encore\Admin\Grid\Exporters;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;

abstract class ExcelExporter extends AbstractExporter implements FromQuery, WithHeadings
{
use Exportable;

/**
* @var string
*/
protected $fileName;

/**
* @var array
*/
protected $headings = [];

/**
* @return array
*/
public function headings(): array
{
return $this->headings;
}

/**
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
*/
public function query()
{
return $this->getQuery();
}

/**
* {@inheritdoc}
*/
public function export()
{
$this->download($this->fileName)->prepare(request())->send();

exit;
}
}

0 comments on commit 03fbcf7

Please sign in to comment.