Laravel Jobs represent discrete units of work that can be executed synchronously or queued for asynchronous processing. They are commonly used for tasks like sending emails, processing uploads, or interacting with external APIs.
Jobs are part of Laravel's queue system, introduced to manage background tasks efficiently and improve application performance.
Create a job:
php artisan make:job SendEmail
In the job class:
namespace App\Jobs;
use Mail;
use App\Models\User;
class SendEmail implements ShouldQueue
{
public function __construct(public User $user) {}
public function handle()
{
Mail::to($this->user->email)->send(new WelcomeMail());
}
}
Dispatch the job:
SendEmail::dispatch($user);
Laravel Jobs streamline background task processing, ensuring efficient and scalable application workflows.