Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renepardon/ec23c8796365ea801e736701807f3879 to your computer and use it in GitHub Desktop.
Save renepardon/ec23c8796365ea801e736701807f3879 to your computer and use it in GitHub Desktop.
<?php
class MyClass
{
public function export()
{
$exportable = $this->prepareData($tags);
// ...
$excel->sheet('Tags', function ($sheet) use ($exportable) {
$sheet->fromGenerator($exportable, null, 'A1', false);
});
// ...
}
/**
* This is how it looks like now
*
* @param array $tags
*
* @return array
*/
protected function prepareArrayData(array $tags): array
{
$data = [];
foreach ($tags as $tag) {
$data[] = [
'ID' => $tag->id,
'Tag' => strip_tags($tag->tag),
'Status' => strip_tags($tag->status),
'Created at' => $tag->created_at,
'Updated at' => $tag->updated_at,
];
}
return $data;
}
/**
* This is how it **SHOULD** look like
*
* @param array $tags
*
* @return array
*/
protected function prepareData(array $tags): array
{
foreach ($tags as $tag) {
yield [
'ID' => $tag->id,
'Tag' => strip_tags($tag->tag),
'Status' => strip_tags($tag->status),
'Created at' => $tag->created_at,
'Updated at' => $tag->updated_at,
];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment