-
Hi, I'm doing a simple import with multiple sheets like this: namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class UsersImport implements WithMultipleSheets
{
public function sheets(): array
{
return [
'first_sheet' => new FirstSheetImport(),
'second_sheet' => new SecondSheetImport(),
];
}
} I'm doing some logic on the FirstSheetImport and need to pass some data to a variable to the SecondSheetImport. I've tried to do this through a session, but that doesn't work. namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class FirstSheetImport implements ToCollection
{
public function collection(Collection $rows)
{
// some import stuff
session()->put('import_data', 'this is my data');
}
} namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class SecondSheetImport implements ToCollection
{
public $my_data;
public function __construct()
{
$this->my_data = session('import_data');
}
public function collection(Collection $rows)
{
// some import stuff
}
} Does anyone know how to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Maybe try |
Beta Was this translation helpful? Give feedback.
-
Hi @patrickbrouwers, thanks for your reply! How do you mean with two separate imports? |
Beta Was this translation helpful? Give feedback.
-
I think I figured it out! The constructor in the sheet imports are for some reason not able to get the data out of the session! I've been using the Events for this now, and it works. It's not really pretty, but it works... namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class FirstSheetImport implements ToCollection
{
public function collection(Collection $rows)
{
// some import stuff
session()->put('foo', 'bar');
}
} namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
class SecondSheetImport implements ToCollection, WithEvents
{
use Importable, RegistersEventListeners;
private static $data;
public static function beforeSheet(BeforeSheet $event)
{
self::$data = session('foo');
}
public function collection(Collection $rows)
{
// some import stuff
ray(self::$data)
}
} |
Beta Was this translation helpful? Give feedback.
-
Thanks @patrickbrouwers for clearing up the contstructor mystery! This made me come up with a really simple solution now: namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class FirstSheetImport implements ToCollection
{
protected $once = false;
public function collection(Collection $rows)
{
$this->runOnce();
// do the import in here
}
private function runOnce()
{
if ($this->once) {
return;
}
// My logoc in here has access to the session now!
$this->once = true;
}
} Thanks again for this great package and awesome documentation @patrickbrouwers ! |
Beta Was this translation helpful? Give feedback.
Thanks @patrickbrouwers for clearing up the contstructor mystery! This made me come up with a really simple solution now:
Thanks again for this great package and awesom…