Answer the question
In order to leave comments, you need to log in
How to create a new column when exporting a table using Laravel, maatwebsite/excel?
Hello! Can you please tell me how to create a new column in the exported (in excel) table? There is a table in the database of the following form:
I installed a package for exporting maatwebsite/excel using composer. There is also a model file:
class ScheduledInspectionModel extends Model
{
protected $table = 'scheduled_inspection'; // название таблицы
protected $fillable = ['name_smp', 'name_control', "verification_start", "verification_end", 'verification_duration'];
public $timestamps = false;
}
class OrganizationsExportController extends Controller
{
public function export()
{
return (new OrganizationsExport)->download('organizations_export.xls');
}
}
class OrganizationsExport implements FromCollection, ShouldAutoSize, WithHeadings, WithEvents
{
use Exportable;
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return ScheduledInspectionModel::all();
}
public function headings(): array
{
return [
'id',
'Проверяемый СМП',
'Контролирующий орган',
'Начало проверки',
'Окончание проверки',
'Плановая длительность'
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getStyle('A1:F1')->applyFromArray([
'font' => [
'bold' => true
]
]);
}
];
}
Answer the question
In order to leave comments, you need to log in
added/updated to the OrganizationsExport
following:
...
private $count = 0;
public function map($organizations): array
{
return [
++$this->count,
$organizations->name_smp,
$organizations->name_control,
$organizations->verification_start . ' - ' . $organizations->verification_end,
$organizations->verification_duration,
];
}
public function headings(): array
{
return [
'№',
'Проверяемый СМП',
'Контролирующий орган',
'Период плановой проверки',
'Плановая длительность',
];
}
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question