laravel-tasks maintained by michal78
Description
Scheduled model tasks for Laravel
Author
Last update
2026/06/28 15:23
(dev-main)
License
Downloads
7
Tags
Laravel Tasks
michal78/laravel-tasks lets you attach scheduled tasks to any Eloquent model.
A task can run one of four target types at a specific time:
- Artisan command
- Action class
- Event class
- Service class method
The model is always passed into the target, and task runs can be logged (optional).
Requirements
- PHP 8.2+ (Laravel 13 requires PHP 8.3+)
- Laravel 11, 12, or 13
Installation
composer require michal78/laravel-tasks
Run migrations:
php artisan migrate
Optional config publish:
php artisan vendor:publish --provider="Michal78\Tasks\TasksServiceProvider" --tag=config
Model Setup
Add the trait to any model:
use Illuminate\Database\Eloquent\Model;
use Michal78\Tasks\Traits\HasTasks;
class User extends Model
{
use HasTasks;
}
Scheduling Tasks
Command task
$user->scheduleCommandTask(
name: 'Sync user',
command: 'users:sync',
runAt: now()->addMinutes(10),
payload: ['--force' => true],
);
The package injects these command options automatically:
--model-type(model class)--model-id(model primary key)
Action task
$user->scheduleActionTask(
name: 'Run action',
actionClass: \App\Actions\UserAction::class,
runAt: now()->addHour(),
payload: ['source' => 'onboarding'],
);
Default method is __invoke. You can pass a custom method with the method argument.
Event task
$user->scheduleEventTask(
name: 'Dispatch event',
eventClass: \App\Events\UserTaskDue::class,
runAt: now()->addMinutes(30),
payload: ['channel' => 'email'],
);
Event constructor signature should accept:
public function __construct(Model $model, array $payload, Task $task)
Service task
$user->scheduleServiceTask(
name: 'Call service',
serviceClass: \App\Services\UserTaskService::class,
runAt: now()->addDay(),
payload: ['dry_run' => false],
method: 'handle', // optional, defaults to handle
);
Running Due Tasks
The package registers:
php artisan tasks:run-due
Add it to Laravel scheduler:
use Illuminate\Support\Facades\Schedule;
Schedule::command('tasks:run-due')->everyMinute();
Task Logging (Optional)
Each run can be logged in task_logs.
Config:
// config/laravel-tasks.php
return [
'logging' => [
'enabled' => env('TASKS_LOGGING_ENABLED', true),
],
];
When enabled, each task run stores status (running, succeeded, failed), start/end timestamps, and optional error message.
Testing
composer test
License
MIT