bitzsalex's avatar

How to Access Importer's email in Maatwebsite Excel Package

I am using Laravel Maatwebsite Excel package to import data from Excel/CSV file using ShouldQueue thread. To get the id and email of importer's I passed the User object to the constructor function of my importer class with the help of Importable thread. But in the afterImport event of my importer class I can't access my user object which I passed to the class via the constructor and stored id in a static field of the class. It keeps giving me access property on a null object.

I tried removing the ShouldQueue thread and upload the Excel file without running a job but still I get get the User's data inside the afterImport function.

Here is my code:

class SchoolsImport implements ToCollection, WithEvents, ShouldQueue
{
	use Importable, RegistersEventListeners;

	protected static $importedBy;
	
	public function __construct(User $importedBy)
    	{
        	self::$importedBy = $importedBy;
    	}

    	public function collection(Collection $collection)
    	{
		dd(self::$importedBy); // Will print the user no matter what
    	}

    	public static function afterImport(AfterImport $event)
    	{
        	$userEmail = self::$importedBy->email; // give me an error
	        Mail::to($userEmail)->send(new Gmail());
	}
}
0 likes
1 reply

Please or to participate in this conversation.