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
[QUESTION] How to return data from implemented import method? #1980
Comments
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:
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? |
|
Awesome! |
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. On the CRUD I am getting: Call to undefined method Maatwebsite\Excel\Excel::getRowCount() I am using a class to call the specfic sheet importer:
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 for first sheet
Page Controller - How can I access id from FirstSheetCommunityImport class?
|
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);
}
// ... |
I'm unable to make it work with ToModel method... any other options? |
I myself solved that problem by creating an array property in the imported class and each time a new item is inserted i push it the result array. example: public $inserted = [];
/**
* @param array $row
*
* @return YourModel|null
*/
public function model(array $row)
{
$inserted = new YourModel([
..
]);
array_push($this->inserted ,$inserted);
return $inserted;
} and then, read that property as bellow: $instance = new YourImportClass;
$imported = Excel::import($instance , $items);
return $instance->inserted; |
I'm unable to make it if I implement WithProgressBar class reference https://docs.laravel-excel.com/3.1/imports/progress-bar.html
|
Prerequisites
Versions
Description
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.
The text was updated successfully, but these errors were encountered: