Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cant use transform in prepareRows #4270

Open
1 task done
Superpat opened this issue Feb 20, 2025 · 2 comments
Open
1 task done

[Bug]: Cant use transform in prepareRows #4270

Superpat opened this issue Feb 20, 2025 · 2 comments
Labels

Comments

@Superpat
Copy link

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.62

What version of Laravel are you using?

11.42.0

What version of PHP are you using?

8.2

Describe your issue

So the docs tell me to use collection transform on the $rows variable, but I'm getting an error saying it's an array ? But if I try using array_map on it, then I get an error telling me it's a collection...

When I use $rows->tranform:

[2025-02-20 18:04:39] local.ERROR: Call to a member function transform() on array {"exception":"[object] (Error(code: 0): Call to a member function transform() on array at /var/www/app/Exports/InventoryExport.php:53)
[stacktrace]
#0 /var/www/vendor/maatwebsite/excel/src/Sheet.php(653): App\\Exports\\InventoryExport->prepareRows(Array)
#1 /var/www/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(81): Maatwebsite\\Excel\\Sheet->appendRows(Array, Object(App\\Exports\\InventoryExport))
#2 /var/www/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php(46): Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->Maatwebsite\\Excel\\Jobs\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#3 /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->Maatwebsite\\Excel\\Jobs\\Middleware\\{closure}()
#4 /var/www/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php(45): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->withLocale(NULL, Object(Closure))
#5 /var/www/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(76): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->handle(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), Object(Closure))
#6 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->handle(Object(Maatwebsite\\Excel\\Writer))
#7 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#8 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#9 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#10 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#11 /var/www/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\\Container\\Container->call(Array)
#12 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#13 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#14 /var/www/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#15 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), false)
#16 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Queue\\CallQueuedHandler->Illuminate\\Queue\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#17 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#18 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(123): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#19 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(71): Illuminate\\Queue\\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#20 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array)
#21 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(440): Illuminate\\Queue\\Jobs\\Job->fire()
#22 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(390): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#23 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(177): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#24 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(148): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#25 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(131): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#26 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Queue\\Console\\WorkCommand->handle()
#27 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#28 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#29 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#30 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#31 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\\Container\\Container->call(Array)
#32 /var/www/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#33 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#34 /var/www/vendor/symfony/console/Application.php(1047): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#35 /var/www/vendor/symfony/console/Application.php(316): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#36 /var/www/vendor/symfony/console/Application.php(167): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#37 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#38 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1205): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#39 /var/www/artisan(13): Illuminate\\Foundation\\Application->handleCommand(Object(Symfony\\Component\\Console\\Input\\ArgvInput))

If I try to use array_map(func, $rows):

[2025-02-20 17:37:58] local.ERROR: array_map(): Argument #2 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given {"exception":"[object] (TypeError(code: 0): array_map(): Argument #2 ($array) must be of type array, Illuminate\\Database\\Eloquent\\Collection given at /var/www/app/Exports/Invent
oryExport.php:55)                                                                                                                                                                                                                                                                                                             
[stacktrace]                                                                                                                                                                                                                                                                                                                  
#0 /var/www/app/Exports/InventoryExport.php(55): array_map(Object(Closure), Object(Illuminate\\Database\\Eloquent\\Collection))                                                                                                                                                                                               
#1 [internal function]: App\\Exports\\InventoryExport->App\\Exports\\{closure}(Object(App\\Models\\Item))                                                                                                                                                                                                                     
#2 /var/www/app/Exports/InventoryExport.php(53): array_map(Object(Closure), Array)                                                                                                                                                                                                                                            
#3 /var/www/vendor/maatwebsite/excel/src/Sheet.php(653): App\\Exports\\InventoryExport->prepareRows(Array)                                                                                                                                                                                                                    
#4 /var/www/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(81): Maatwebsite\\Excel\\Sheet->appendRows(Array, Object(App\\Exports\\InventoryExport))                                                                                                                                                                  
#5 /var/www/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php(46): Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->Maatwebsite\\Excel\\Jobs\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))                                                                                                           
#6 /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->Maatwebsite\\Excel\\Jobs\\Middleware\\{closure}()                                                                                                                                  
#7 /var/www/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php(45): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->withLocale(NULL, Object(Closure))                                                                                                                                                            
#8 /var/www/vendor/maatwebsite/excel/src/Jobs/AppendDataToSheet.php(76): Maatwebsite\\Excel\\Jobs\\Middleware\\LocalizeJob->handle(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), Object(Closure))                                                                                                                      
#9 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Maatwebsite\\Excel\\Jobs\\AppendDataToSheet->handle(Object(Maatwebsite\\Excel\\Writer))                                                                                                                                                    
#10 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()                                                                                                                                                                           
#11 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))                                                                                                                                                                             
#12 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#13 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#14 /var/www/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\\Container\\Container->call(Array)                                                                                                                                                                                                   
#15 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#16 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#17 /var/www/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))                                                                                                                                                                                           
#18 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet), false)
#19 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Queue\\CallQueuedHandler->Illuminate\\Queue\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#20 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#21 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(123): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))                                                                                                                                                                                  
#22 /var/www/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(71): Illuminate\\Queue\\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Maatwebsite\\Excel\\Jobs\\AppendDataToSheet))
#23 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): Illuminate\\Queue\\CallQueuedHandler->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array)
#24 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(440): Illuminate\\Queue\\Jobs\\Job->fire()                                                                                                                                                                                                              
#25 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(390): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#26 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(177): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#27 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(148): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#28 /var/www/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(131): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#29 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Queue\\Console\\WorkCommand->handle()                                                                                                                                                                                         
#30 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()                                                                                                                                                                           
#31 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))                                                                                                                                                                             
#32 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#33 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#34 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\\Container\\Container->call(Array)                                                                                                                                                                                                  
#35 /var/www/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#36 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#37 /var/www/vendor/symfony/console/Application.php(1047): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#38 /var/www/vendor/symfony/console/Application.php(316): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#39 /var/www/vendor/symfony/console/Application.php(167): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#40 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#41 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1205): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#42 /var/www/artisan(13): Illuminate\\Foundation\\Application->handleCommand(Object(Symfony\\Component\\Console\\Input\\ArgvInput))                                                                                                                                                                                           
#43 {main}                                                                     
"}                                          

How can the issue be reproduced?

This was the simplest example I could setup:

    public function prepareRows($rows)
    {
        return $rows->transform(function ($item) {
            $item->name .= 'test';
            return $item;
        });
    }

I'm open to making a full scenario if theres a default sandbox tool I can use, but I want to make sure I'm not simply holding it wrong first.

What should be the expected behaviour?

The transform method should work on $rows.

@Superpat Superpat added the bug label Feb 20, 2025
@Superpat
Copy link
Author

I realize now that this is probably due to me using FromCollection instead of FromQuery, so I'll close this. But it's still a confusing error.

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Feb 21, 2025

I ll leave it open, because I don't think there should be a difference. I don't know how it exactly work do I can't really give an answer now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants