1. Go to this page and download the library: Download x-laravel/listmonk 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/ */
x-laravel / listmonk example snippets
use XLaravel\Listmonk\Contracts\NewsletterSubscriber;
use XLaravel\Listmonk\Traits\InteractsWithNewsletter;
class User extends Authenticatable implements NewsletterSubscriber
{
use InteractsWithNewsletter;
}
class User extends Authenticatable implements NewsletterSubscriber
{
use InteractsWithNewsletter;
// Custom email column
protected string $newsletterEmailColumn = 'email_address';
// Custom name column
public function getNewsletterNameColumn(): string
{
return 'full_name';
}
// Custom list IDs
public function getNewsletterLists(): array
{
$lists = [1]; // Main newsletter
if ($this->is_premium) {
$lists[] = 2; // Premium list
}
return $lists;
}
// Custom attributes synced to Listmonk
public function getNewsletterAttributes(): array
{
return [
'plan' => $this->subscription_plan ?? '',
'country' => $this->country ?? '',
'registered_at' => $this->created_at?->toIso8601String(),
];
}
// Custom passive list per model
public function getNewsletterPassiveListId(): ?int
{
return 5;
}
}
User::withoutNewsletterSync(function () {
$user->update(['email' => '[email protected]']);
// No Listmonk API calls will be made
});
use XLaravel\Listmonk\Events\SubscriberSubscribed;
class SendWelcomeEmail
{
public function handle(SubscriberSubscribed $event): void
{
$model = $event->model;
$apiResponse = $event->response;
}
}
use XLaravel\Listmonk\Testing\InteractsWithListmonk;
class MyTest extends TestCase
{
use InteractsWithListmonk;
public function test_user_subscribes(): void
{
$this->fakeListmonk();
$user = User::factory()->create();
$this->assertSubscriberSynced($user->email);
}
public function test_with_existing_subscriber(): void
{
$this->fakeListmonkWithSubscriber([
'id' => 1,
'email' => '[email protected]',
'name' => 'John',
'lists' => [['id' => 1]],
'attribs' => [],
'status' => 'enabled',
]);
// ...
}
public function test_api_failure(): void
{
$this->fakeListmonkFailure(500, 'Internal Server Error');
// ...
}
}