| Install | |
|---|---|
composer require hlacos/joboquent |
Eloquent based Jobs for Laravel
It's under development, not recommended for production use!
Tipically it creates in the controller.
$job = new JobModel;
$job->name = 'Export customers';
$job->save();
$job->run('MyJob');
The string parameter of the run method is the class name of the Worker in the next step.
Feel free to extend this model, just read the extended model section in the worker.
Extend Job to make your own working code
use Hlacos\Joboquent\Job;
class MyJob extends Job {
// Callbacks
public function beforeStart() {}
public function beforeEnd() {}
// The working code
public function work() {}
}
Only override the $jobModelClass public attribute name to the Extended class name.
Tipically used in the work method in a cycle.
$this->jobModel->setPercent($percent);
You can set polimorphic relation to the JobModel.
public function jobs() {
return $this->morphMany('Hlacos\Joboquent\JobModel', 'jobable');
}
public function job() {
return $this->morphOne('Hlacos\Joboquent\JobModel', 'jobable');
}
Don't forget to save related model to the jobModel before it runs.