PHP code example of getsendstack / laravel-sendstack

1. Go to this page and download the library: Download getsendstack/laravel-sendstack 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/ */

    

getsendstack / laravel-sendstack example snippets


declare(strict_types=1);

use App\Models\Subscriber;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use SendStack\Laravel\Contracts\ClientContract;

namespace App\Jobs\SendStack;

class SyncSubscribers implements ShouldQueue
{
    use Queueable;
    use Dispatchable;
    use SerializesModels;
    use InteractsWithQueue;
    
    public function handle(ClientContract $client): void
    {
        foreach ($client->subscribers()->all() as $subscriber) {
            Subscriber::query()->updateOrCreate(
                attributes: ['email' => $subscriber->email],
                values: $subscriber->toArray(),
            );
        }
    }
}

declare(strict_types=1);

use App\Models\Subscriber;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use SendStack\Laravel\Facades\SendStack;

namespace App\Jobs\SendStack;

class SyncSubscribers implements ShouldQueue
{
    use Queueable;
    use Dispatchable;
    use SerializesModels;
    use InteractsWithQueue;
    
    public function handle(): void
    {
        foreach (SendStack::subscribers()->all() as $subscriber) {
            Subscriber::query()->updateOrCreate(
                attributes: ['email' => $subscriber->email],
                values: $subscriber->toArray(),
            );
        }
    }
}

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->all();

/**
 * Using the Facade
 */
SendStack::subscribers()->all();

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->get(
    query: '1234-1234-1234-1234' // This can be either the subscribers UUID or their Email Address
);

/**
 * Using the Facade
 */
SendStack::subscribers()->get(
    query: '1234-1234-1234-1234', // This can be either the subscribers UUID or their Email Address
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;
use SendStack\Laravel\Http\Requests\SubscriberRequest;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->create(
    request: new SubscriberRequest(
        email: '[email protected]', // Required
        firstName: 'Send', // Optional
        lastName: 'Stack', // Optional
        tags: [
            'Client',
            'Awesome',
        ], // Optional
        optIn: true, // Optional
    ),
);

/**
 * Using the Facade
 */
SendStack::subscribers()->create(
    request: new SubscriberRequest(
        email: '[email protected]', // Required
        firstName: 'Send', // Optional
        lastName: 'Stack', // Optional
        tags: [
            'Client',
            'Awesome',
        ], // Optional
        optIn: true, // Optional
    ),
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;
use SendStack\Laravel\Http\Requests\SubscriberRequest;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->update(
    uuid: '1234-1234-1234-1234',
    request: new SubscriberRequest(
        email: '[email protected]', // Required
        firstName: 'Send', // Optional
        lastName: 'Stack', // Optional
        tags: [
            'Client',
            'Awesome',
        ], // Optional
        optIn: true, // Optional
    ),
);

/**
 * Using the Facade
 */
SendStack::subscribers()->update(
    uuid: '1234-1234-1234-1234',
    request: new SubscriberRequest(
        email: '[email protected]', // Required
        firstName: 'Send', // Optional
        lastName: 'Stack', // Optional
        tags: [
            'Client',
            'Awesome',
        ], // Optional
        optIn: true, // Optional
    ),
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->delete(
    uuid: '1234-1234-1234-1234'
);

/**
 * Using the Facade
 */
SendStack::subscribers()->delete(
    uuid: '1234-1234-1234-1234',
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->attachTag(
    uuid: '1234-1234-1234-1234',
    tag: 'Early Access',
);

/**
 * Using the Facade
 */
SendStack::subscribers()->attachTag(
    uuid: '1234-1234-1234-1234',
    tag: 'Early Access',
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->subscribers()->removeTag(
    uuid: '1234-1234-1234-1234',
    tag: 'Early Access',
);

/**
 * Using the Facade
 */
SendStack::subscribers()->removeTag(
    uuid: '1234-1234-1234-1234',
    tag: 'Early Access',
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->isActiveSubscriber(
    email: '[email protected]',
);

/**
 * Using the Facade
 */
SendStack::isActiveSubscriber(
    email: '[email protected]',
);

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->tags()->all();

/**
 * Using the Facade
 */
SendStack::tags()->all();

use SendStack\Laravel\Contracts\ClientContract;
use SendStack\Laravel\Facades\SendStack;
use SendStack\Laravel\Http\Requests\TagRequest;

/**
 * Without a Facade
 */
$client = app()->make(
    abstract: ClientContract::class,
);

$client->tags()->create(
    request: new TagRequest(
        name: 'Test', // Required
        allowFormSubscription: true, // Optional
    ),
);

/**
 * Using the Facade
 */
SendStack::tags()->create(
    request: new TagRequest(
        name: 'Test', // Required
        allowFormSubscription: true, // Optional
    ),
);
bash
php artisan vendor:publish --tag="sendstack-config"