| Version | Date | Notes | By |
|---|---|---|---|
| 0.1 | 2017-05-19 | Initial release | ROB |
Spout is a plugin that focuses more on speed and less on formating options for the resulting excel spreadsheet. To use this exporter you must create a few files. We will start with the service php file you need to create that extends the SpoutExcelExporter (we will be using the families exporter as an example):
<?php
namespace WeMake\Services\Documents;
use Illuminate\Database\Eloquent\Collection;
use WeMake\Http\Requests\Request;
use WeMake\Services\Core\SpoutExcelExporter;
/**
* Class FamiliesListingExporter
*
* @package OneVet\Interfaces
*/
class FamiliesListingExporter extends SpoutExcelExporter
{
/**
* @var string
*/
protected $filename = 'families_listing'; // name of the generated file (no extension)
/**
* @var string
*/
protected $type = 'xlsx'; // defines wether it exports an excel file, csv file or ods file
/**
* @var array
*/
protected $header = [ // array of elements for the header (translated strings)
'form.field.name',
'form.field.type',
...
];
}
If you need to export as CSV file, just change the type variable above to csv.
After creating the file above, you must create an ExportTransformer (if you have read the PDF Listing Exporter documentation you will notice it is the same type of file):
<?php
namespace WeMake\Transformers\Documents;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\TransformerAbstract;
use WeMake\Contracts\Core\CollectionTransformerContract;
/**
* Class FamilyExportTransformer
*
* @package WeMake\Transformers\Documents
*/
class FamilyExportTransformer extends TransformerAbstract implements CollectionTransformerContract
{
/**
* Turn this item object into a generic array
*
* @param Collection $collection
*
* @return array
*/
public function transform(Collection $collection)
{
$records = [];
foreach ($collection as $record) {
$records[] = $this->transformRecord($record);
}
return $records;
}
/**
* Turn this item object into a generic array
*
* @param Model $model
*
* @return array
*/
private function transformRecord(Model $model)
{
return [
'name' => $model->name,
'type' => $type,
...
];
}
}
Now, just alter the appropriate Repository (in this case it would be the FamiliesRepository) and add this function: NOTE: This document presuposes that you know what a repository (backend) is and what it is for
/**
* Returns export records.
*
* @param Request $request
* @param CollectionTransformerContract $transformer
*
* @return array $records
*/
public function export(Request $request, CollectionTransformerContract $transformer)
// (Note: if you need to export more than one diferent excel list name your functions like exportExcelName1, exportExcelName2, ...)
{
$records = $this->searchQuery($request)->get();
return $transformer->transform($records); // return an array of transformed records
}
Finally, just add a method to the controller that allows the generated file to be downloaded:
/**
* Creates excel file and downloads it to browser
*
* @param ShowRequest $request
* @param Repository $repository
* @param FamiliesListingExporter $exporter
*/
public function excel(ShowRequest $request, Repository $repository, FamiliesListingExporter $exporter)
// Note: Again, change the name of the function as appropriate
{
$records = $repository->export($request, new FamilyExportTransformer);
$exporter->download($request, $records); // generates and downloads the excel file.
}