AlexanderKim
1 year ago

How to pass request data to job, then to mailable class

Posted 1 year ago by AlexanderKim

Mailable class:

class CallBack extends Mailable
{
    use Queueable, SerializesModels;

    public $name;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name)
    {
        $this->name = $name;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.callback');
    }
}

Job class:

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

    protected $name;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($name)
    {
        $this->name = $name;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $settings = Settings::find(1);
        Mail::to($settings->site_email)->send(new CallBackMail($name));
    }
}

Controller:

CallBack::dispatch($request->input('name'));

Then i get the error about undefined variable $name, simple things is complicated with jobs, wtf.

Please sign in or create an account to participate in this conversation.