I'm just wondering if there's any way to communicate with my controller from the implemented import method. I've tried returning a data array to the controller but it's not getting through. Haven't seen any mention of this...
I know I can use toCollection and return the entire import to a collection in the controller but worried about taking a performance hit.
Put the imported data into a class property of your import object and make a getter for it that you can call after the import has finished iin your controller.
I must still be missing something, let me get a little more specific.
So here's my import call in my controller:
$import = Excel::import(new SalesImport, 'imports/salesdata.xlsx');
class SalesImport implements ToCollection
{
public function collection(Collection $rows)
{
.... lots of other stuff ....
$this->data = $data;
}
It's clear to me that this data isn't making it's way back to my $import object in the controller, but I'm guessing I need to implement something other than ToCollection here?
class SalesImport implements ToCollection
{
public $data;
public function collection(Collection $rows)
{
.... lots of other stuff ....
$this->data = $data;
}
$import = new SalesImport;
Excel::import($import, 'imports/salesdata.xlsx');
dd($import->data);
class SalesImport implements ToCollection { public $data; public function collection(Collection $rows) { .... lots of other stuff .... $this->data = $data; }
$import = new SalesImport; Excel::import($import, 'imports/salesdata.xlsx'); dd($import->data);
Awesome!
class SalesImport implements ToCollection { public $data; public function collection(Collection $rows) { .... lots of other stuff .... $this->data = $data; }$import = new SalesImport; Excel::import($import, 'imports/salesdata.xlsx'); dd($import->data);
Can you please set that in https://docs.laravel-excel.com/3.1/imports/collection.html or another place in documentation? I lost a lot of time searching that.
A similar example has been in the docs for a while: https://docs.laravel-excel.com/3.1/architecture/objects.html#getters
Great library, this example works great for a single sheet.
Any tips on implementing with WithMultipleSheets import? How should I get the row count?
On the CRUD I am getting: Call to undefined method MaatwebsiteExcelExcel::getRowCount()
I am using a class to call the specfic sheet importer:
class ExcelSampleSheetsImport implements WithMultipleSheets
{
protected $params;
public function __construct($params)
{
$this->params = $params;
}
public function sheets(): array
{
return [
'Sheet to import' => new ExcelSampleImport($this->params)
];
}
}
On ExcelSampleImport class I have implemented the counter as specified on the docs.
How I can do this but with multiple sheets?
Import class
class CommunityImport implements WithMultipleSheets
{
use WithConditionalSheets;
public function conditionalSheets(): array
{
return [
0 => new FirstSheetCommunityImport(),
1 => new SecondSheetCommunityImport(),
];
}
}
Class for first sheet
class FirstSheetCommunityImport implements ToCollection
{
public $id;
public function collection(Collection $rows)
{
$i=0;
foreach ($rows as $row)
{
if($i==0){
$this->id = $row[0];
}
$i++;
}
}
}
Page Controller - How can I access id from FirstSheetCommunityImport class?
public function import()
{
$import = new CommunityImport();
$import->onlySheets(0);
Excel::import($import, storage_path('app/xls/').'comunidad-import.xlsx');
}
To access that id from the FirstSheetCommunityClass was an issue for me but I used Laravel cache to store my collection for 10 minute and then access it from the controller using the Cache Facade
Here is my take on getters with multiple sheets. Using cache for this is not a good idea in my opinion...
<?php
// VendorsExtraDataImportSheet.php:
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class VendorsExtraDataImportSheet implements ToCollection
{
public $count = 0;
public function collection(Collection $rows)
{
$this->count++;
}
}
<?php
// VendorsExtraDataImport.php
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class VendorsExtraDataImport implements ToCollection, WithMultipleSheets
{
use WithConditionalSheets;
public const SHEET_TO_IMPORT = 'Example data input';
public $sheet;
public function collection(Collection $rows)
{
}
public function conditionalSheets(): array
{
if (!$this->sheet) {
$this->sheet = new VendorsExtraDataImportSheet;
}
return [
self::SHEET_TO_IMPORT => $this->sheet,
];
}
}
<?php
// ...
// PageController.php
public function import()
{
$import = new VendorsExtraDataImport();
$import->onlySheets(VendorsExtraDataImport::SHEET_TO_IMPORT);
Excel::import($import, resource_path('data/vendors.xlsx'));
dd($import->sheet->count);
}
// ...
Most helpful comment