Laravel Job

mxl/laravel-job image

Laravel Job stats

Downloads
515.6K
Stars
56
Open Issues
1
Forks
7

View on GitHub →

Laravel job tools

laravel-job

Laravel job tools:

  • dispatch job from command line with parameters to queue or run synchronously;
  • Job base class with boilerplate.

Installation

$ composer require mxl/laravel-job

Laravel 5.5+ will use the auto-discovery feature to add MichaelLedin\LaravelJob\LaravelJobServiceProvider::class to providers.

This package is not compatible with older Laravel versions.

Usage

Dispatching job from command line to the queue

Make sure that you either use sync connection (see default property in config/queue.php) or run queue worker:

$ php artisan queue:work

Then dispatch command with:

$ php artisan job:dispatch YourJob

if YourJob class is located under \App\Jobs or specify full class name with namespace:

$ php artisan job:dispatch '\Path\To\YourJob'

Running jobs immediately

If you want to run job right now without posting it to queue use job:dispatchNow command:

$ php artisan job:dispatchNow YourJob

Dispatching jobs with parameters

$ php artisan job:dispatch YourJob John 1990-01-01

John and 1990-01-01 values will be passed to YourJob constructor as $name and $birthDate arguments:

class YourJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
__constructor($name, $birthDate)
{
// ...
}
 
public function handle()
{
// ...
}
}

Using job with parameters from command line and PHP code

Often a job is already in use somewhere from PHP code and if it has constructor arguments that must have specific type, then it can be required to parse command line parameters.

For this purpose implement FromParameters interface:

use MichaelLedin\LaravelJob\FromParameters;
use Carbon\Carbon;
 
class YourJob implements ShouldQueue, FromParameters
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
__constructor(string $name, Carbon $birthDate)
{
// ...
}
 
public function handle()
{
// ...
}
 
public static function fromParameters(...$parameters)
{
return new self($parameters[0], Carbon::parse($parameters[1]));
}
}

Job boilerplate

Job classes always use the same interface ShouldQueue and Dispatchable, InteractsWithQueue, Queueable, SerializesModels traits.

To avoid such boilerplate your jobs can extend MichaelLedin\LaravelJob\Job class:

use MichaelLedin\LaravelJob\Job;
use Carbon\Carbon;
 
class YourJob extends Job
{
// ...
}

It also includes default FromParameters interface implementation that is equivalent to calling constructor with arguments provided by command line parameters without any parsing.
To add parsing override fromParameters method.

Maintainers

Other useful Laravel packages from the author

License

See the LICENSE file for details.

mxl photo

Chief technology officer at metapax.io

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Mxl Laravel Job Related Articles

Building and Deploying a Laravel App With Claude Code on Zerops image

Building and Deploying a Laravel App With Claude Code on Zerops

Read article
RouteKey Model Attribute in Laravel 13.21 image

RouteKey Model Attribute in Laravel 13.21

Read article
Laravel Time Machine: A Request Lifecycle Profiler image

Laravel Time Machine: A Request Lifecycle Profiler

Read article
The Analytics Stack for Laravel: Traffic, Revenue and Attribution in One Place image

The Analytics Stack for Laravel: Traffic, Revenue and Attribution in One Place

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
HTTP Query Method Support in Laravel 13.19 image

HTTP Query Method Support in Laravel 13.19

Read article
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises