SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.3k stars 1.93k forks source link

Headings not being added when using queue. #1921

Closed bugzbrown closed 5 years ago

bugzbrown commented 5 years ago

Versions

Description

Exporting Excel files with the WithHeadings concern does not generate a header when excel file is queued.

My exporter implements FromQuery, WithHeadings

and when I call it using store('file.xlsx')

it generates an excel perfectly with a header line.

if I call it with queue('file.xlsx') it will generate the file without the header.

Steps to Reproduce

Using a query with a simple table for example: name, age 'fred', 32; 'john', 43;

My Exporter:

use App\ItemModel;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ItemExport implements FormQuery, WithHeadings
{
  use Exportable;
  public function headings() : array
  {
    return [ 'name', 'age' ];
  }
  public function query()
  {
    return ItemModel::query()->where('age','>',20);
  }
}

My controller:

// ... 
// WORKING export
public function export() // route - items/export
{
  $export = (new ItemExport)->store('myItems.xlsx');
}
//  export without HEADINGS
public function exportq() // route - items/exportq
{
  $export = (new ItemExport)->queue('myItems-queue.xlsx');
}

If you visit both URLs you should have two stored excels in your storage folder...

Expected behavior:

myItems.xlsx

A B
1 name age
2 fred 32
3 john 43

and

myItems-queue.xlsx

A B
1 name age
2 fred 32
3 john 43

Actual behavior:

myItems.xlsx

A B
1 name age
2 fred 32
3 john 43

and

myItems-queue.xlsx

A B
1 fred 32
2 john 43

Additional Information

I haven't quite figured out how you go about putting the header on the file yet... so I haven't come up with a PR - if you can help guide me to where I am supposed to look at, I can try and send one in when I come up with a solution.

bugzbrown commented 5 years ago

Something interesting... I found where the problem lies!

in lines 144 -> 149 of file Sheet.php you add headings within an array:

        if (!$sheetExport instanceof FromView && $sheetExport instanceof WithHeadings) {
            if ($sheetExport instanceof WithCustomStartCell) {
                $startCell = $sheetExport->startCell();
            }
            $this->append([$sheetExport->headings()], $startCell ?? null, $this->hasStrictNullComparison($sheetExport));
        }

If left like this, when you use store() everything works OK If you use queue() - it does not add a heading...

If you now remove the array around headings(), the opposite occurs

Replacing the if for this will do the trick:

        if (!$sheetExport instanceof FromView && $sheetExport instanceof WithHeadings) {
            if ($sheetExport instanceof WithCustomStartCell) {
                $startCell = $sheetExport->startCell();
            }
            if ($sheetExport instanceof ShouldQueue){
                $this->append($sheetExport->headings(), $startCell ?? null, $this->hasStrictNullComparison($sheetExport));
            }else{
                $this->append([$sheetExport->headings()], $startCell ?? null, $this->hasStrictNullComparison($sheetExport));
            }
        }
GlennM commented 5 years ago

Hi @bugzbrown ,

I'm unable to reproduce your issue. My code:

<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;

class UserController extends Controller
{
    public function export()
    {
        (new UsersExport())->queue('users-export.xlsx');
    }
}
<?php

namespace App\Exports;

use App\User;
use Illuminate\Database\Query\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;

class UsersExport implements FromQuery, WithHeadings
{
    use Exportable;

    public function headings(): array
    {
        return [
            'id',
            'name',
            'email address',
            'date created',
            'updated',
            'something else',
        ];
    }

    public function query()
    {
        return User::query()->where('id', '>', '5');
    }
}

I do see that in the code snippet you posted, you're referring to FormQuery instead of FromQuery. Could that perhaps be the issue?

GlennM commented 5 years ago

Closing due to inactivity.

aceraven777 commented 3 months ago

I am also having this issue when I upgraded from L10 to L11.

Versions PHP version: 8.3 Laravel version: 11 Package version: 3.1

I'm using export ->queue($filename)->onQueue("QueueName")

The weird thing is, in my local environment its working as expected, but when I deployed to vapor, the heading is not being exported.

bpudenz commented 3 months ago

Any update here? I'm seeing the exact same thing. K8s environments are missing headings only, and local is fine even when queueing.

PHP 8.3 Laravel version 10 Package 3.1.55