I'm trying to take an export made using FromArray and convert to using FromQuery, the thing is I'm adding images to my excel but idk how to change it so that it works with the query instead, this is the code. I'm going to remove the methods that don't have any issues, like WithColumnWidths, WithStyles, and WithHeading. The commented out stuff is how I had it with FromArray.
class SurveyExport implements FromQuery, WithMapping, WithColumnWidths, ShouldAutoSize, WithHeadings, WithEvents, WithStyles
{
use Exportable;
// protected $items;
// protected $include_images;
//
// public function __construct(array $items, $include_images)
// {
// $this->items = $items;
// $this->include_images = $include_images;
// }
protected $request;
protected $id;
public function __construct(Request $request, $id)
{
$this->request = $request;
$this->id = $id;
}
public function query()
{
$survey = Survey::findOrFail($this->id);
$filters = json_decode($this->request->get('filters'));
$result = SurveyReportDetail::with(
'question',
'section',
'report.user',
'report.survey.sections.questions',
'report.sale_point.format.chain.channel'
)->whereHas('report', function ($query) use ($filters, $survey) {
$query->salePointFilter($filters, $survey->id);
$query->dateRangeFilter($filters);
$query->supervisorFilter($filters);
});
if ($filters->images) {
$result->whereHas('report.survey.sections.questions', function ($query) {
$query->where('response_type', '!=', 'image');
});
}
return $result;
}
public function map($row): array
{
$filters = json_decode($this->request->get('filters'));
$output = [
date('Y-m-d H:i:s', strtotime($row['report']['date'])),
$row['report']['latitude'],
$row['report']['longitude'],
!empty($row['report']['sale_point']['format']['chain']['channel']['description']) ?
$row['report']['sale_point']['format']['chain']['channel']['description'] : '',
!empty($row['report']['sale_point']['format']['chain']['description']) ?
$row['report']['sale_point']['format']['chain']['description'] : '',
!empty($row['report']['sale_point']['format']['description']) ?
$row['report']['sale_point']['format']['description'] : '',
!empty($row['report']['sale_point']['name']) ?
$row['report']['sale_point']['name'] : '',
$row['report']['user']['name'],
$row['section']['description'],
$row['question']['description']
];
if ($row['question']['response_type'] === 'image' &&
file_exists(public_path('img/surveys/' . $row['value'])) &&
$filters->include_images
) {
$output[] = '';
} else {
$output[] = $row['value'];
}
if ($row['report']['survey']['is_evaluation']) {
$output[] = $row['grade'];
}
return $output;
}
// public function array(): array
// {
// $data = [];
//
// foreach ($this->items as $item) {
// BASICALLY SAME CODE AS MAP METHOD
// }
//
// return $data;
// }
public function getDrawings(): array
{
$drawings = [];
foreach ($this->items as $index => $item) {
if ($item['question']['response_type'] === 'image' &&
file_exists(public_path('img/surveys/' . $item['value'])) &&
$this->include_images
) {
$drawing = new Drawing();
$drawing->setName('Imagen');
$drawing->setPath(public_path('/img/surveys/' . $item['value']));
$drawing->setHeight(36);
$drawing->setCoordinates('K' . ($index + 2));
$drawings[] = $drawing;
}
}
return $drawings;
}
/**
* @return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
foreach ($this->items as $index => $item) {
if ($item['question']['response_type'] === 'image' &&
file_exists(public_path('img/surveys/' . $item['value'])) &&
$this->include_images
) {
$event->sheet->getDelegate()->getRowDimension($index + 2)->setRowHeight(50);
}
}
foreach ($this->getDrawings() as $drawing) {
$drawing->setWorksheet($event->sheet->getDelegate());
}
},
];
}
}
I was using the array of $this->items to check the placement of my images and also to make the whole row taller to acomodate the image size, how can I do that using the FromQuery?