Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

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

Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

Read article
Debounceable Queued Jobs in Laravel 13.6.0 image

Debounceable Queued Jobs in Laravel 13.6.0

Read article
An Opinionated Agent Skill for Building REST APIs in Laravel image

An Opinionated Agent Skill for Building REST APIs in Laravel

Read article
Redis Cluster Support for Queues in Laravel 13.5.0 image

Redis Cluster Support for Queues in Laravel 13.5.0

Read article
Debugging production Laravel apps without losing your mind image

Debugging production Laravel apps without losing your mind

Read article
FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0 image

FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0

Read article
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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge