michal78 / laravel-tasks
Scheduled model tasks for Laravel
Requires
- php: ^7.4|^8.0|^8.1|^8.2
- illuminate/cache: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^6.0 || ^10.0
- phpunit/phpunit: ^9.0 || ^10.0
- dev-main
- 1.0.0.3
- 1.0.0.2
- 1.0.0.0
- dev-codex/tests-workflow
- dev-codex/fresh-model-scheduled-tasks
- dev-codex/add-task-timing-and-scheduling-feature
- dev-xzw7yl-codex/update-import-statement-and-fix-code-block
- dev-codex/make-tasks-owner-polymorphic
- dev-codex/remove-danish-locale-from-taskfactory
- dev-codex/find-and-fix-important-bug
- dev-codex/update-import-statement-and-fix-code-block
- dev-codex/fix-typo-in-comment
- dev-codex/add-tests-for-hastasks-model
- dev-codex/enable-support-for-laravel-12
- dev-Test_workflow
- dev-2-initial-fixes-after-irl-testing
- dev-michal78-patch-1
- dev-Api_and_routes
This package is auto-updated.
Last update: 2026-04-11 09:22:03 UTC
README
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 11 or 12
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