PHP code example of ismail-rt / laravel-atlas-drip

1. Go to this page and download the library: Download ismail-rt/laravel-atlas-drip 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/ */

    

ismail-rt / laravel-atlas-drip example snippets


// THE NAIVE PATTERN (DANGEROUS IN PRODUCTION)
User::where('email_verified_at', '<=', now()->subDays(3))->get()->each(function ($user) {
    Mail::to($user)->send(new OnboardingMail());
});

return [
    /*
    |--------------------------------------------------------------------------
    | Engine Enabled
    |--------------------------------------------------------------------------
    */
    'enabled' => env('LAD_ENABLED', true),

    /*
    |--------------------------------------------------------------------------
    | Global Inter-Campaign Cooldown (Hours)
    |--------------------------------------------------------------------------
    | If a recipient received ANY lifecycle email in the last X hours,
    | subsequent campaign emails are paused to prevent inbox bombing.
    */
    'global_cooldown_hours' => (int) env('LAD_COOLDOWN_HOURS', 48),

    /*
    |--------------------------------------------------------------------------
    | Historical Enrollment Cutoff
    |--------------------------------------------------------------------------
    | Optional timestamp (e.g. '2026-09-01 00:00:00'). Recipients whose anchor
    | is prior to this date are rejected from enrollment unless a prior notification
    | log proves they were already in the campaign.
    */
    'enrollment_started_at' => env('LAD_ENROLLMENT_STARTED_AT', null),

    /*
    |--------------------------------------------------------------------------
    | Default Recipient Model
    |--------------------------------------------------------------------------
    */
    'recipient_model' => env('LAD_RECIPIENT_MODEL', App\Models\User::class),

    /*
    |--------------------------------------------------------------------------
    | Database Table Names
    |--------------------------------------------------------------------------
    */
    'table_names' => [
        'states' => env('LAD_TABLE_STATES', 'lad_campaign_states'),
        'logs' => env('LAD_TABLE_LOGS', 'lad_notification_logs'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Signed Unsubscribe Routing
    |--------------------------------------------------------------------------
    */
    'routes' => [
        'enabled' => true,
        'prefix' => 'lad',
        'middleware' => ['web'],
    ],
];

use Lad\Facades\Drip;
// Aliases available:
// use Lad\Facades\Lifecycle;
// use Lad\Facades\Lad;

use Lad\Campaign;
use Lad\Step;

// 1. Onboarding Campaign
Drip::register(
    Campaign::make('onboarding')
        ->priority(10) // Lower number = evaluated first
        // Who qualifies for this campaign?
        ->eligible(fn ($user) => $user->hasVerifiedEmail() && !$user->isAdmin())
        // What is the reference time clock?
        ->anchor(fn ($user) => $user->email_verified_at)
        // Maximum age for initial enrollment (failsafe against first-deploy explosions)
        ->maxEnrollmentAgeDays(10)
        // Goal completion: cancel when user achieves desired action
        ->cancelWhen(fn ($user) => $user->properties()->exists())
        // Sequential steps
        ->steps([
            Step::make('day3')
                ->offsetDays(3)
                ->notification(OnboardingReminderNotification::class),

            Step::make('day5')
                ->offsetDays(5)
                ->minimumGapDays(2)
                ->notification(OnboardingTipsNotification::class),

            Step::make('day7')
                ->offsetDays(7)
                ->minimumGapDays(2)
                ->notification(function ($user) {
                    return new OnboardingDiscountNotification(promoCode: 'WELCOME15');
                }),
        ])
);

// 2. Recurring Streak Campaign (e.g., Idle Win-Back)
Drip::register(
    Campaign::make('idle_winback')
        ->priority(20)
        ->eligible(fn ($user) => $user->properties()->exists())
        ->anchor(fn ($user) => $user->last_login_at)
        // Custom instance key isolates recurring streaks
        ->instanceKey(fn ($user) => $user->last_login_at?->utc()->format('Y-m-d-H-i-s'))
        ->cancelWhen(fn ($user) => $user->last_login_at?->gt(now()->subDays(7)))
        ->steps([
            Step::make('idle_reminder')
                ->offsetDays(7)
                ->notification(WeMissYouNotification::class),
        ])
);

Step::make('welcome_hour2')
    ->offsetHours(2)
    ->minimumGapHours(1)
    ->notification(QuickStartNotification::class);

use Illuminate\Support\Facades\Schedule;

Schedule::command('drip:send')->hourly();

use Illuminate\Support\Facades\Event;
use Lad\Events\StepDispatched;

Event::listen(StepDispatched::class, function (StepDispatched $event) {
    Log::info("Dispatched {$event->campaign->getName()}:{$event->step->getKey()} to #{$event->recipient->id}");
});
mermaid
flowchart TD
    Start(["⏰ Cron: php artisan drip:send"]) --> OptCheck{"1. Opted Out?"}

    OptCheck -->|"Yes"| SkipOpt["🚫 Skip Recipient<br/>(Opt-Out Ledger Active)"]
    OptCheck -->|"No"| CoolCheck{"2. In 48h Cooldown?"}

    CoolCheck -->|"Yes"| SkipCool["🛑 Pause Delivery<br/>(Cross-Campaign Flood Guard)"]
    CoolCheck -->|"No"| EnrollCheck{"3. Qualifies for Enrollment?"}

    EnrollCheck -->|"Exceeds Max Age"| SkipEnroll["🛡️ Reject Enrollment<br/>(First-Deploy Blast Guard)"]
    EnrollCheck -->|"Active / Enrolled"| GoalCheck{"4. Goal Reached?"}

    GoalCheck -->|"Yes (cancelWhen)"| CancelState["🎯 Cancel Campaign<br/>(Terminal State: No Zombies)"]
    GoalCheck -->|"No"| TimingCheck{"5. Step Due? (Two-Clock)"}

    TimingCheck -->|"Not Yet"| WaitTiming["⏳ Wait for Due Date<br/>(Jitter & Lag Guard)"]
    TimingCheck -->|"Due"| DedupeTx{"6. Atomic DB Deduplication"}

    DedupeTx -->|"Key Exists (Race)"| SkipRace["⚡ Skip Silently<br/>(Zero Double-Sends)"]
    DedupeTx -->|"Lock Acquired"| Dispatch["📨 Dispatch Notification / Mailable"]

    Dispatch --> StateUpdate["Advance Campaign State<br/>(Record step & timestamp)"]
bash
php artisan vendor:publish --tag=lad-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=lad-config
php artisan vendor:publish --tag=lad-views
bash
php artisan drip:status 42

# Aliases:
php artisan lifecycle:status 42
php artisan lad:status 42
bash
# Cancel all active campaigns for recipient #42
php artisan drip:cancel 42

# Cancel a specific campaign
php artisan drip:cancel 42 onboarding

# Aliases:
php artisan lifecycle:cancel 42
php artisan lad:cancel 42
bash
vendor/bin/pest
# Or via artisan:
php artisan test