elwan7dev's avatar

issue with laravel Excel package: when try to inject dependencies and Chunk , queue import class -> trying to queue and chunkReading import class

laravel-version : 6.x php-version : 7.4

Imports/QuestionnaireImport.php

class QuestionnaireImport implements ToCollection, WithMultipleSheets, WithChunkReading, ShouldQueue
{

// use Importable;
	private $questionnaire;
	private $arrange;
    private $campaignService ,$channelService,$leadIdsService ,$inventoryItemService;
	public $collection;
	public $rows;
	public $questionnaireService;
    private $requestTypeService;

	public function __construct(Questionnaire $questionnaire, Array $arrange, TagServices $tag_service, StateService $state_service, $questionnaireService ,ChannelService $channelService,
    CampaignService $campaignService,InventoryItemService $inventoryItemService,LeadIdsService $leadIdsService ,RequestTypeService $requestTypeService)
	{

		$this->questionnaire = $questionnaire;
		$this->arrange = $arrange;
		$this->tag_service = $tag_service;
		$this->state_service = $state_service;
		$this->questionnaireService = $questionnaireService;
        $this->campaignService = $campaignService;
		$this->channelService = $channelService;
        $this->leadIdsService = $leadIdsService;
        $this->requestTypeService =$requestTypeService;
        $this->inventoryItemService = $inventoryItemService;
	}


 public function collection(Collection $collection){

// importing logic that uses dependencies

}

public function sheets(): array
    {
        return [
            0 => $this
        ];
    }
    public function chunkSize(): int
    {
        return 1000;
    }



}

and init QuestionnaireImport in Services/LeadService.php and passes all parameters

class LeadService
{

public $repo;
    private $answer_service;
    private $returned_service;
    public $request_service;
    public $requestTypeService;
    private $req_salesman_service;
    private $request_log_service;
    private $task_service;
    public $questionnaire;
    private $LeadIds_repository;
    private $user_service;
    private $notificationService;
    private $leadModel;
    private $leadIdsSercice;
    private $sms_service;
    private $coldCallService;
    private $campaign_repo;
    private $state_service;
    private $channelService;
    private $campaignService;
    private $inventoryItemService;
    private $branchService;
    private $tags_service;
    private $commentService;
    private $userModel;

    /**
     * Create a new Repository instance.
     *
     * @param LeadRepository $repo
     * @return void
     */
    public function __construct(LeadRepository $repository, AnswerService $answer_service,
                                ReturnedService $returned_service, RequestService $request_service,
                                RequestSalesManService $req_salesman_service, RequestLogService $request_log_service,
                                StateService $state_service, TaskService $task_service, LeadIdsRepository $LeadIds_repository,
                                UserService $userService,LeadIdsService $leadIdsSercice,ColdCallService $coldCallService,
                                NotificationService $notificationService, Lead $leadModel, TagServices $tags_service,
                                AnswerTaggingService $answer_tagging_service , SMSTemplateService $sms_service,
                                LastSalesmanService $lastSalesmanService, LeadsRotationService $leadsRotationService,
                                CampaignRepository $campaign_repo, RequestTypeService $requestTypeService,CommentService $commentService,
                                QuestionnaireService $questionnaireService, ChannelService $channelService,LeadIdsService $leadIdsService,
                                CampaignService $campaignService, InventoryItemService $inventoryItemService, BranchService $branchService,
                                User $userModel
    )
    {
        $this->repo = $repository;
        $this->user_service = $userService;
        $this->answer_service = $answer_service;
        $this->returned_service = $returned_service;
        $this->request_service = $request_service;
        $this->req_salesman_service = $req_salesman_service;
        $this->request_log_service = $request_log_service;
        $this->state_service = $state_service;
        $this->task_service = $task_service;
        $this->LeadIds_repository = $LeadIds_repository;
        $this->notificationService = $notificationService;
        $this->leadModel = $leadModel;
        $this->tags_service = $tags_service;
        $this->leadIdsSercice = $leadIdsSercice;
        $this->answer_tagging_service = $answer_tagging_service;
        $this->sms_service = $sms_service;
        $this->coldCallService = $coldCallService;
        $this->lastSalesmanService = $lastSalesmanService;
        $this->leadsRotationService = $leadsRotationService;
        $this->requestTypeService = $requestTypeService;
        $this->leadIdsService = $leadIdsService;
        // Get the Leads Questionnaire
        $this->questionnaire = getQuestionnaire();
        $this->campaign_repo = $campaign_repo;
        $this->questionnaireService = $questionnaireService;
        $this->channelService = $channelService;
        $this->campaignService = $campaignService;
        $this->inventoryItemService = $inventoryItemService;
        $this->branchService = $branchService;
        $this->commentService = $commentService;
        $this->userModel = $userModel;
    }


// some logic
// get all rows into the first sheet
        $import = new QuestionnaireImport($this->questionnaire(), $request->arrange, $this->tags_service, $this->state_service, $questionnaireService ,$this->channelService,$this->campaignService,$this->inventoryItemService,$this->leadIdsService ,$this->requestTypeService);
        // get the first sheet rows
        Excel::import($import, $file);
        $rows = $import->rows;

// some logic

}

return this exception when uploading enter image description here enter image description here

https://flareapp.io/share/RmrDDRq7

failed solutions

1- try to inject dependencies into handle function() but can't used in collection and can't pass it so use an app()->make() helper follow docs https://laravel.com/docs/9.x/queues#class-structure

public function handle()
    {
// code 
        $tag_service =  app()->make(TagServices::class);
// code
    }

initialized correctly but cannot use in the collection function

2- try to inject dependencies in constructor using app()->make() helper

public function __construct($request)
	{
        $this->arrange = $request['arrange'];
        $this->questionnaire =  app()->make(QuestionnaireService::class)->find_by(new Request(['related_type' => 'Sales\Models\Lead']))->first();
        $this->tag_service =  app()->make(TagServices::class);
        $this->state_service =  app()->make(StateService::class);
        $this->questionnaireService = app()->make(QuestionnaireService::class);
        $this->campaignService = app()->make(CampaignService::class);
        $this->channelService = app()->make(ChannelService::class);
        $this->leadIdsService = app()->make(LeadIdsService::class);
        $this->requestTypeService = app()->make(RequestTypeService::class);
        $this->inventoryItemService = app()->make(InventoryItemService::class);

    }

return the same exception Exception Serialization of 'Closure' is not allowed

0 likes
0 replies

Please or to participate in this conversation.