Laravel jobs dispatchNow

I have been using dispatchNow to trigger Jobs in laravel 7 but i recently switched to laravel 8 and when i try usingdispatchNow in laravel 8 things don’t work and in the editer am using it croses dispatchNow and then it says ‘dispatchNow’ is deprecated.
How can i solve this.This is my code of how am doing it.

  $job = new AccountCreatedEmailJob($user->contact, $message);
        $this->dispatchNow($job);

>Solution :

To dipatch Jobs in Laravel 8 use the following example.

  AccountCreatedEmailJob::dispatch(
    new AccountCreatedEmail($request->email, $body,$subject), $request->email);

And in your AccountCreatedEmailJob Do this

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

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

    public $mailable, $email;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->email)->send($this->mailable);
    }
}

Leave a Reply