I have one to many relationship between two tables one is "alquileres" (rents) and the other is "cobros" (payments). I need to show the totals of each rental like this:
what i need
But obtain this:
result
This is the code of my export:
class ComprobantesExport implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize, WithEvents
{
use Exportable, RegistersEventListeners;
protected $alquileres;
public function __construct($alquileres = null)
{
$this->alquileres = $alquileres;
}
/**
* @return IlluminateSupportCollection
*/
public function collection()
{
return $this->alquileres;
}
public function headings(): array
{
return [
'#',
'Fecha',
'Inquilino',
'Propiedad',
'Subpropiedad',
'Atraso',
'Monto'
];
}
public function map($alquiler): array
{
return $alquiler->cobros->map(function ($cobro, $nro) use ($alquiler){
return [
$cobro->comprobante->id,
$cobro->created_at->format("d/m/Y"),
$alquiler->inquilino->full_name,
$alquiler->propiedad->titulo,
$alquiler->subpropiedad->titulo,
'si',
$alquiler->moneda->simbolo . $cobro->comprobante->monto,
];
})->toArray();
}
/**
* @return array
*/
public function registerEvents(): array
{
return [
BeforeExport::class => function(BeforeExport $event) {
$event->writer->getProperties()->setCreator('Sistema de alquileres');
},
];
}
}
My controller:
public function comprobantes()
{
$alquileres = Alquiler::with('moneda', 'inquilino', 'propiedad', 'subpropiedad', 'cobros')
->get();
return(new ComprobantesExport($alquileres))->download('comprobantes.xlsx');
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…