lopatin96/laravel-campaign

Laravel campaign

1.0.14 2024-02-05 22:11 UTC

This package is auto-updated.

Last update: 2024-12-05 23:58:39 UTC


README

Migrations

php artisan vendor:publish --tag="laravel-campaign-migrations"

then run php artisan migrate

Config

Publish config to manage active mails:

php artisan vendor:publish --tag="laravel-campaign-config"

Trait and Casts

Add HasCampaign trait and casts to User model.

use Atin\LaravelCampaign\Traits\HasCampaign;

class User extends Authenticatable
{
    use HasCampaign;
   
    protected $casts = [
         'campaign_unsubscribed_at' => 'datetime',
    ];
}

Generating Mailables

New "mailable" class will be stored in the app/Mail directory.

php artisan make:mail TestMail
use Atin\LaravelMail\Mail\Mailable;

class TestMail extends Mailable
{
    public function build()
    {
         // Build email
    }
}

Campaigns

Create app/Campaigns directory and Campaign class:

use Atin\LaravelCampaign\Campaigns\Campaign;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Builder;

class TestCampaign extends Campaign
{
    protected string $mailable = '\App\Mail\TestMail';
    
    protected function buildQuery(): Builder
    {
         return \App\Models\User::where(function($query) {
            $query->whereDate('users.trial_ends_at', '>=', now())
                ->whereDate('users.trial_ends_at', '<', now()->addDay());
        });
    }
}
<?php

return [
    'active_mails' => [
        '\App\Campaigns\TestCampaign' => 'daily',
    ]
];

Don't forget to add your campaign class to laravel-campaign config.

Schedule

Register schedule task in app/Concole/Kernel.php

use Atin\LaravelCampaign\Console\SendCampaignEmails;

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule): void
    {
        $schedule->call(new SendCampaignEmails())
            ->hourly();
    }

Publishing

Config

php artisan vendor:publish --tag="laravel-campaign-config"

Views

php artisan vendor:publish --tag="laravel-campaign-views"

Localization

php artisan vendor:publish --tag="laravel-campaign-lang"

Migrations

php artisan vendor:publish --tag="laravel-campaign-migrations"