This example shows a job class SendWelcomeEmail that takes a user and pretends to send an email by printing a message. Then it dispatches the job with a user email.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendWelcomeEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function handle()
{
// Imagine sending email here
echo "Email sent to {$this->user['email']}\n";
}
}
// Dispatching the job
$user = ['email' => 'friend@example.com'];
SendWelcomeEmail::dispatch($user);