0

I've spent hours finding out why the excel export with the package cyber-duck/laravel-excel export OK the excel when the datasource is a query, but when using a custom serialiser it simply stops formatting the excel correctly

No errors in code, super simple excel. Even trying the code posted in the documentation:

Usage:

$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$excel->setSerialiser($serialiser);
return $excel->stream('filename.xlsx');

CustomSerialiser:

namespace App\Serialisers;

use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;

class ExampleSerialiser implements SerialiserInterface
{
    public function getData($data)
    {
        $row = [];

        $row[] = $data->field1;
        $row[] = $data->relationship->field2;

        return $row;
    }

    public function getHeaderRow()
    {
        return [
            'Field 1',
            'Field 2 (from a relationship)'
        ];
    }
}

Any thoughts?

leopinzon
  • 707
  • 6
  • 13
  • Since https://github.com/Cyber-Duck/laravel-excel seems abandoned, you could consider using this alternative that uses spout too: https://github.com/rap2hpoutre/fast-excel/blob/master/README.md – rap-2-h Aug 30 '18 at 07:55

1 Answers1

0

What software do you use to open the file? Excel? OpenOffice?

If you open the test folder > Unit > ExporterTest.php, you should see a working example in test_can_use_a_custom_serialiser.

You can change row 155 to $exporter = $this->app->make('cyber-duck/exporter')->make('Excel');, row 160 to $reader = ReaderFactory::create(Type::XLSX); (otherwise it would use a CSV) and comment out line 174 to keep the file so you can open it after the test has running.

The ExampleSerialiser you posted needs to be modified to match your Eloquent model and relationships. Also, the example use an Eloquent version and you mentioned the query builder. If you want to use the Query builder version, you need to use loadQuery (I'll try to update the documentation next week to cover this user case). Feel free to drop me an email with your code so I can have a look and try to help out (It's a bit hard to understand the issue without seeing the actual implementation). You should find me on github, I'm one of the Cyber-Duck guys working on our projects.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129