PHP code example of lacodix / laravel-notification-subscriptions

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

    

lacodix / laravel-notification-subscriptions example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Lacodix\LaravelNotificationSubscriptions\Traits\HasNotificationSubscriptions;

class User extends Authenticatable
{
    use Notifiable;
    use HasNotificationSubscriptions;
}

use App\Models\User;
use App\Notifications\InvoicePaid;

$user = User::findOrFail(1);

$user->unsubscribe(InvoicePaid::class);         // Global wildcard: all channels.
$user->subscribe(InvoicePaid::class, 'mail');   // Explicit mail exception.
$user->unsubscribe(InvoicePaid::class, 'mail'); // Change only the mail rule.
$user->subscribe(InvoicePaid::class);           // Change the wildcard rule.

use App\Notifications\InvoicePaid;

$user->resetSubscriptions(InvoicePaid::class);
$user->resetSubscriptions(InvoicePaid::class)->subscribe(InvoicePaid::class);



namespace App\Notifications;

use App\Models\Organization;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function __construct(
        public Organization $organization,
        public array $optIn = [],
        public bool $ignoreSubscriptions = false,
    ) {
    }

    public function via($notifiable): array
    {
        return ['mail'];
    }

    public function toMail($notifiable): MailMessage
    {
        return (new MailMessage())
            ->subject('Invoice paid')
            ->line('An invoice for your organization has been paid.');
    }

    public function getSubscriptionModel($notifiable): ?Model
    {
        return $this->organization;
    }

    public function getOptInSubscriptions(): array
    {
        return $this->optIn;
    }
}

use App\Models\Organization;
use App\Models\User;
use App\Notifications\InvoicePaid;

$user = User::findOrFail(1);
$organization = Organization::findOrFail(1);

$user->unsubscribe(InvoicePaid::class, '*', $organization);
$user->subscribe(InvoicePaid::class, 'mail', $organization);
$user->notify(new InvoicePaid($organization)); // Mail is allowed by the scoped exception.

$user->resetSubscriptions(InvoicePaid::class, $organization);

use App\Notifications\InvoicePaid;

$notification = new InvoicePaid($organization, optIn: ['mail']);
$user->resetSubscriptions(InvoicePaid::class, $organization);
$user->resetSubscriptions(InvoicePaid::class);
$user->notify($notification); // Suppressed: no mail or wildcard subscription exists.

$user->subscribe(InvoicePaid::class, 'mail', $organization);
$user->notify($notification); // Allowed by the scoped mail subscription.

use App\Notifications\InvoicePaid;

// Resolved decision, including wildcard/global fallback and an explicit opt-in list.
$allowed = $user->isSubscribed(InvoicePaid::class, 'mail', $organization, ['mail']);

// Exact tuple only: returns a NotificationSubscription or null, with no fallback.
$rule = $user->findSubscription(InvoicePaid::class, 'mail', $organization);

// Query all preferences, or just this scope, using the Eloquent relation.
$allRules = $user->notificationSubscriptions()->get();
$scopedRules = $user->notificationSubscriptions()->model($organization)->get();

use App\Models\User;
use App\Notifications\InvoicePaid;

$users = User::with('notificationSubscriptions')->get();

foreach ($users as $user) {
    $allowed = $user->isSubscribed(InvoicePaid::class, 'mail');
}



return [
    'excluded_channels' => ['database'],
];

use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($organization, ignoreSubscriptions: true));

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Lacodix\LaravelNotificationSubscriptions\Events\NotificationSuppressed;

Event::listen(NotificationSuppressed::class, function (NotificationSuppressed $event): void {
    Log::info('Notification suppressed', [
        'notification' => get_class($event->notification),
        'channel' => $event->channel,
    ]);
});
bash
php artisan vendor:publish --provider='Lacodix\LaravelNotificationSubscriptions\NotificationSubscriptionsServiceProvider'
bash
php artisan event:clear
# Or rebuild for your deployment:
php artisan event:cache