@mstdmstd use fromArray instead
https://docs.laravel-excel.com/3.1/exports/collection.html#using-arrays
Or build your collection before you return it https://docs.laravel-excel.com/3.1/exports/collection.html#using-custom-structures
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In Laravel 8 with "maatwebsite/excel": "^3.1" I make export of table row with features that some enum columns must have label instead of enum values like :
public function collection()
{
$adsIdList = [7,1,5];
return Ad
::getById($adsIdList)
->get()
->map(function ($adItem) {
$a = Carbon::createFromFormat('Y-m-d', $adItem->expire_date);
$is_past = $a->isPast();
$retAdItem = [
'id' => $adItem->id, // A
'title' => $adItem->title, // B
'slug' => $adItem->slug, // C
'phone_display' => $adItem->phone_display ? 'Y' : 'N', // D
'has_locations' => $adItem->has_locations ? 'Y' : 'N', // E
'status' => Ad::getStatusLabel($adItem->status), // F
'price' => $adItem->price, // G
'ad_type' => Ad::getAdTypeLabel($adItem->ad_type), // H
'expire_date' => $adItem->expire_date, // I
'is_past' => $is_past, // ?
also I add some calculative fields in my app/Exports/AdsExport.php file above.
But if there is a way to add data from related table? Any Ad can have several ad_locations with several fields... Later I will need to write import functionality to generated in export files...
Thanks!
Please or to participate in this conversation.