'Show drawing conditionally in laravel-excel
I have an Export which looks like this:
class CatalogExport implements WithHeadings, WithDrawings, WithMapping, FromCollection
and I want the Drawing to be shown conditionally, but I don't really know how to do it.
I tried it this way:
public function drawings()
{
$drawing = new Drawing();
if($this->semnat === 1)
{
$drawing->setName('Semnatura');
$drawing->setDescription('This is my logo');
$drawing->setHeight(100);
$drawing->setCoordinates('F3');
$drawing->setPath(null);
$drawing->setPath(storage_path('app/public/' . $this->imgPath));
}
return $drawing;
}
For any other value than 1 the fields will remain not completed, so I thought there might be no drawing shown. But then I get this error:
File not found!
So, is there any way to show this drawing conditionally, other than creating two export classes, one which implements WithDrawings and one which doesn't? (but would create a lot of duplicate code)
Solution 1:[1]
It looks like there's some code missing from your question.
Regardless, I'd rejig your drawing function to be like this:
public function drawings()
{
if($this->semnat === 1 && $this->imgPath)
{
$drawing = new Drawing();
$drawing->setName('Semnatura');
$drawing->setDescription('This is my logo');
$drawing->setHeight(100);
$drawing->setCoordinates('F3');
$drawing->setPath(null);
$drawing->setPath(storage_path('app/public/' . $this->imgPath));
return $drawing;
}
return null;
}
This code checks to see if imgPath is set, and also returns null if semnat isn't 1 or imgPath is not set.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joundill |
