PHP code example of mortenscheel / laravel-automation

1. Go to this page and download the library: Download mortenscheel/laravel-automation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mortenscheel / laravel-automation example snippets


protected function schedule(Schedule $schedule)
{
    // ...
    $schedule->command('automation:run');
}

Automation::create([
    'trigger_class' => ModelAgeTrigger::class,
    'trigger_params' => [
        'model' => User::class,
        'age' => 60 * 15,
    ],
    'action_class' => SendMailableAction::class,
    'action_params' => [
        'mailable' => WelcomeEmail::class,
        'mailable_params' => [
            'name',
        ],
    ],
]);

class ModelAgeTrigger extends \Scheel\Automation\AutomationTrigger
{
    public function discoverAutomatable(Automation $automation): Collection
    {
        $class = $this->params->get('model');
        return $class::query()->where('created_at', '<=', now()->subSeconds($this->params->get('age')))
            ->whereDoesntHave('automationLogs', fn ($logs) => $logs->where('automation_id', $automation->id))
            ->get();
    }
}

class SendMailableAction extends \Scheel\Automation\AutomationAction
{
    protected function executeAction(): bool
    {
        $params = $this->log->automation->action_params;
        $mailable_class = $params->get('mailable');
        $recipient = $this->log->automatable;
        $mailable_params = [];
        foreach ($params->get('mailable_params', []) as $mailable_param) {
            $mailable_params[] = data_get($recipient, $mailable_param);
        }
        $mailable = new $mailable_class(...$mailable_params);
        \Mail::to($recipient)->send($mailable);
        return true;
    }
}
bash
php artisan vendor:publish --tag="automation-migrations"
php artisan migrate