Home > Enterprise >  Laravel Job Withcount Parameter not Accessible in job Laravel 5.6
Laravel Job Withcount Parameter not Accessible in job Laravel 5.6

Time:06-29

Hii I created a job to send mail. I execute a query in the controller using withCount() and am able to get the count parameter inside the controller but while I am parsing the same data to the job and assign to a local variable in constructer then I use a local variable inside the handle method count parameter was missing in my local.

I am using SYNC as QUEUE_DRIVER and code files below mention

Controller

 $confernceIterationData = ConferenceIteration::with('AbstractNews')->withCount('AbstractNews')->where('id', $unserializeData['confid'])->first();
              
 $this->dispatch(new SendtronAutomatedEmailJob($confernceIterationData, $unserializeData, $attachments));

Controller Output of dd($confereceIterationData)

array:42 [
    "id" => 9085
    "conference_iteration_id" => "e3f65fda-7776-4e64-82d8-b5f1289141e2"
    "conference_id" => 259
    "name" => "American Association of Cancer Research Annual Meeting 2022"
    "acronym" => "AACR 2022"
    "abstract_news_count" => 8339
  ]

Job

<?php

namespace App\Jobs;

use App\Mail\SendTronAutomatedMail;
use App\TeamConferences;
use App\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;

class SendtronAutomatedEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * conference Iteration data
     *
     * @var object
     */
    protected $confrenceData;

    /**
     * Planner Form Data
     *
     * @var array
     */
    private $formData;

    /**
     * attchement files
     *
     * @var array
     */
    private $files;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($confrenceData, $formData, $files)
    {
        $this->confrenceData = $confrenceData;
        $this->confrenceData->abcount = $confrenceData->abstract_news_count;
        $this->formData = $formData;
        $this->files = $files;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        dd($this->confrenceData);

        $request = $this->formData;
        $i = 0;
        $users = User::select('email')->distinct()->wherehas('teams', function ($q) use ($request) {
            $q->whereIn('id', $request['teams']);
        })->pluck('email')->toarray();

        $subject = $this->confrenceData->acronym . " Conference Planner (" . $this->confrenceData->data_status . ")";

        

        $this->extractConfenceData($this->confrenceData);

      
 
        // dd($this->confrenceData->AbstractNews_count);

        $count = count($users);
        foreach ($users as $user) {
            Log::channel('sendtron_email')->info('Authomated Mail Send To .', ['id' => $user]);

            Mail::to($user)->send(new SendTronAutomatedMail($request['content'], $subject, $this->files));
            if (  $i === $count) {
                unset($users);
                TeamConferences::where('conference_iteration_id', $request['confid'])->whereIn('team_id', $request['teams'])->update(['last_mail_sent' => Carbon::now()->toDateTimeString()]);
                Storage::disk('s3SendTron')->delete(array_column($this->files, 's3path'));
            }
        }

    }
}

If we dd($confrenceData) in constructor we get the attribute abstract_news_count as mention in controller output.

but at the same time if we dd($this->confrenceData) we unable to get abstract_news_count

CodePudding user response:

if you use supervisor, you try restarting supervisor If you use cli, you should turn off and run php artisan queue:work Or put params to one array.

CodePudding user response:

I declare another variable to class and set the value inside the constructer in job. so code looks like

public function __construct($confrenceData, $formData, $files)
{
    $this->confrenceData = $confrenceData;
    $this->confrenceData->abcount = $confrenceData->abstract_news_count;
    $this->formData = $formData;
    $this->files = $files;
}
  • Related