Laravel-excel: [BUG] The map() method argument is an array

Created on 30 Apr 2018  路  9Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [x] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [x] Checked that your issue isn't already filed.
  • [x] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.2.4
  • Laravel version: 5.6.17
  • Package version: ^3.0

Description

Exporting with Laravel Excel throws an exception coming from the map() method.

Steps to Reproduce

Controller code:

class TestController  extends Controller {

   public function export() 
   {
       (new \App\Exports\ProductExport)->queue('test.xlsx')->chain([
            new NotifyUserOfCompletedExport( request()->user() ),
       ]);
   }

Export class :

namespace App\Exports;

use Maatwebsite\Excel\Concerns\{
    Exportable,
    FromCollection,
    WithHeadings,
    WithMapping  
};

class ProductExport implements FromCollection, WithMapping, WithHeadings {
    use Exportable;

    public function collection()
    {
        return Product::all();
    }

    public function map($product): array
    {
        return [ 
            'id' => $product->id
        ];
    }
}

Expected behavior:

Should write an excel file with the first column as the product id.

Actual behavior:

Get an exception because the $product argument of the method map() is an array and not an object as the docs say (see ref image)

Exception

[2018-04-30 03:28:16] local.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at /Users/corrado/Sites/store/app/Exports/ProductExport.php:57)
[stacktrace]
#0 /Users/corrado/Sites/store/app/Exports/ProductExport.php(57): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/Users/corrado/...', 57, Array)
#1 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Sheet.php(247): App\\Exports\\ProductExport->map(Array)
#2 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(68): Maatwebsite\\Excel\\Sheet->appendRows(Array, Object(App\\Exports\\ProductExport))
#3 [internal function]: Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->handle(Object(Maatwebsite\\Excel\\Writer))
#4 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#5 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#6 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#7 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#8 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\\Container\\Container->call(Array)
#9 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#10 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#11 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#12 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), false)
#13 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(83): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array)
#14 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(322): Illuminate\\Queue\\Jobs\\Job->fire()
#15 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(272): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#16 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#17 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#18 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(85): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#19 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#20 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#21 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#22 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#23 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#24 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call(Array)
#25 /Users/corrado/Sites/store/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#26 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#27 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(865): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#28 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(241): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#29 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(143): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 /Users/corrado/Sites/store/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 {main}
"} 
[2018-04-30 03:28:16] local.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at /Users/corrado/Sites/store/app/Exports/ProductExport.php:57)
[stacktrace]
#0 /Users/corrado/Sites/store/app/Exports/ProductExport.php(57): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/Users/corrado/...', 57, Array)
#1 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Sheet.php(247): App\\Exports\\ProductExport->map(Array)
#2 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(68): Maatwebsite\\Excel\\Sheet->appendRows(Array, Object(App\\Exports\\ProductExport))
#3 [internal function]: Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->handle(Object(Maatwebsite\\Excel\\Writer))
#4 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#5 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#6 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#7 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#8 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\\Container\\Container->call(Array)
#9 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#10 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#11 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#12 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), false)
#13 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(83): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array)
#14 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(322): Illuminate\\Queue\\Jobs\\Job->fire()
#15 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(272): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#16 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#17 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#18 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(85): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#19 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#20 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#21 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#22 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#23 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#24 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call(Array)
#25 /Users/corrado/Sites/store/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#26 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#27 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(865): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#28 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(241): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#29 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(143): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 /Users/corrado/Sites/store/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 {main}
"} 
[2018-04-30 03:28:16] local.ERROR: Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at /Users/corrado/Sites/store/app/Exports/ProductExport.php:57)
[stacktrace]
#0 /Users/corrado/Sites/store/app/Exports/ProductExport.php(57): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/Users/corrado/...', 57, Array)
#1 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Sheet.php(247): App\\Exports\\ProductExport->map(Array)
#2 /Users/corrado/Sites/store/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(68): Maatwebsite\\Excel\\Sheet->appendRows(Array, Object(App\\Exports\\ProductExport))
#3 [internal function]: Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->handle(Object(Maatwebsite\\Excel\\Writer))
#4 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#5 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#6 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#7 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#8 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\\Container\\Container->call(Array)
#9 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#10 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#11 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#12 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), false)
#13 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(83): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array)
#14 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(322): Illuminate\\Queue\\Jobs\\Job->fire()
#15 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(272): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#16 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#17 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#18 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(85): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#19 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#20 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#21 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#22 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#23 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#24 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call(Array)
#25 /Users/corrado/Sites/store/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#26 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#27 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(865): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#28 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(241): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#29 /Users/corrado/Sites/store/vendor/symfony/console/Application.php(143): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 /Users/corrado/Sites/store/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 /Users/corrado/Sites/store/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 {main}
"} 

Checking product argument type

public function map($product): array
{
    \Log::info(gettype($product);

    return [
       'id' => $product['id'] // works.
    ];
}

# laravel.log
array
array
array

Additional Information

* Ref docs where means the map() method. Image

bug

All 9 comments

Fixed in 3.0.4 (https://github.com/Maatwebsite/Laravel-Excel/releases/tag/3.0.4)

@patrickbrouwers problem solved! Thanks!

@patrickbrouwers Sorry, but should not have the same behavior when using the FromQuery / FromView Trait?

I don鈥檛 understand your question, can you explain more

The same issue that I explained in this issue it's happening again when implementing FromQuery. Before FromCollection had the same behavior but you fixed it.

Code

class InvoicesExport implements FromQuery, WithMapping

    /**
    * @var Invoice $invoice
    */
    public function map($invoice): array
    {

        return [
            $invoice->invoice_number,
            Date::dateTimeToExcel($invoice->created_at),
        ];
    }
}

Bug
The $invoice in map() should be an object but it is an array.

@patrickbrouwers - I am not sure if this is a bug or my ignorance of how to use this tool. If it is a bug, I believe it is related to this issue. I am trying to use withMapping along side fromView. I do not get any errors, and the export occurs. But the map functionality does not correctly transform the mysql datetime data to excel datetime.

Here is my export logic:

<?php

namespace App\Exports;

use App\PD;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Maatwebsite\Excel\Concerns\WithMapping;

class PDExport implements FromView, ShouldAutoSize, WithMapping
{
    use Exportable;

    public function __construct(PD $pD)
    {
        $this->pd = $pD;
    }

    public function view(): View 
    {
        return view('exports.registrants', [
            'pd' => $this->pd,
            'registrants' => PD::find($this->pd->id)->registrants,
        ]);

    }

    public function map($pd): array
    {
        return [
            $this->pd->title,
            $this->pd->id,
            Date::dateTimeToExcel($this->pd->start_dateTime),
            Date::dateTimeToExcel($this->pd->end_dateTime),
        ];
    }
}

My controller logic:

use App\PD;
use App\Exports\PDExport;
...
public function export_registrants(PD $pD) {
        return (new PDExport($pD))->download('PD_Registration.xlsx'); 
    }

My view template:

<table>
    <thead>
        <tr>
            <td>Registation List For:</td>
            <td>Start Date/Time:</td>
            <td>End Date/Time:</td>
        </tr>
        <tr>
            <th>{{ $pd->title}}</th>
            <th>{{ $pd->start_dateTime }}</th>
            <th>{{ $pd->end_dateTime }}</th>
        </tr>
        <tr></tr>
        <tr>
            <th>Name</th>
            <th>Primary Email</th>
            <th>Secondary Email</th>
            <th>Session Completed</th>
        </tr>
    </thead>
    <tbody>
        @foreach($registrants as $r)
        <tr>
            <td>{{ $r->name }}</td>
            <td>{{ $r->email }}</td>
            <td>{{ $r->email2 }}</td>
            <td>hello</td>
        </tr>
        @endforeach
    </tbody>
</table>

@bmangus you can't use them together.

That explains it. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abbylovesdon picture abbylovesdon  路  25Comments

launtony picture launtony  路  29Comments

dalholm picture dalholm  路  24Comments

saeedvz picture saeedvz  路  22Comments

shineraj94 picture shineraj94  路  25Comments