Sorry for the long wait @nakov
Your advice worked nicely. You just forgot to mention I need to add use Maatwebsite\Excel\Concerns\WithHeadings; to the export file.
I actually have my export like this:
<?php
namespace App\Exports;
use App\Contract;
use App\ContractHistory;
use App\ActiveContract;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\View\View;
#use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class ContractsExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
{
use Exportable;
protected $contractids;
protected $active;
public function __construct($contractids = null, $active = null)
{
$this->contractids = $contractids;
$this->active = $active;
}
/*public function view(): View
{
$contracts = $this->contracts;
$active = $this->active;
return view("exports.contracts", compact("contracts", "active"));
}*/
public function headings(): array
{
return [
'#',
'RUT',
'Apellido paterno',
'Apellido materno',
'Nombre',
'Cargo',
'Fecha de inicio',
'Fecha de término',
'Causal de contratación',
'Tope del causal',
'Lugar de trabajo',
'Cliente',
'Estado',
];
}
public function collection()
{
switch ($this->active) {
case 'Solicitudes de Contrataciones':
$dbtable = Contract::whereIn('status', ['pending', 'approved', 'process'])
->whereIn('id', $this->contractids);
break;
case 'Contrataciones activas':
$dbtable = ActiveContract::where('status', "active");
break;
case 'Historial de contrataciones':
$dbtable = ContractHistory::whereIn('status', ['']);
break;
}
return $dbtable->get()->map(function($contract) {
if ($this->active == "Solicitudes de Contrataciones") {
$startdate = $contract->search->start_date;
$enddate = $contract->search->end_date;
}else {
$startdate = $contract->start_date;
$enddate = $contract->end_date;
}
switch ($contract->status) {
case 'pending':
$status = "Por aprobar";
break;
case 'approved':
$status = "Aprobado";
break;
case 'process':
$status = "En proceso";
break;
case 'cancelled':
$status = "Cancelada";
break;
case 'rejected':
$status = "Rechazada";
break;
case 'completed':
$status = "Conpletada";
break;
case 'nullified':
$status = "Anulada";
break;
}
return [
'#' => $contract->id,
'RUT' => $contract->rut,
'Apellido paterno' => $contract->f_lname,
'Apellido materno' => $contract->f_lname,
'Nombre' => $contract->name,
'Cargo' => $contract->search->position->name,
'Fecha de inicio' => $startdate,
'Fecha de término' => $enddate,
'Causal de contratación' => $contract->search->causal_service,
'Tope del causal' => $contract->name,
'Lugar de trabajo' => $contract->search->address,
'Cliente' => $contract->search->client->name,
'Estado' => $status,
];
});
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(13);
},
];
}
}
I still need to make some adjustments for making it work in every .blade.php I need it, but it's now working.
Thank you very much for your help.