PHP code example of aa-engineering / laravel-scheduled-reminders

1. Go to this page and download the library: Download aa-engineering/laravel-scheduled-reminders 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/ */

    

aa-engineering / laravel-scheduled-reminders example snippets


return [
    'prune_after_days' => env('REMINDERS_PRUNE_AFTER_DAYS', 30),
    'log_errors' => env('REMINDERS_LOG_ERRORS', true),
    'queue_connection' => env('REMINDERS_QUEUE_CONNECTION', null),
    'queue_name' => env('REMINDERS_QUEUE_NAME', 'default'),
];

use AAEngineering\ScheduledReminders\Traits\Remindable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Remindable;
    
    // ...
}

use App\Notifications\TaskDueNotification;

$user = User::find(1);
$task = Task::find(1);

// Schedule a reminder for tomorrow
$user->remindAt(
    notification: TaskDueNotification::class,
    source: $task,
    sendAt: now()->addDay(),
    data: ['custom' => 'data']
);

// Get all reminders
$allReminders = $user->reminders;

// Get pending reminders (due but not sent)
$pendingReminders = $user->pendingReminders;

// Get future reminders (scheduled for later)
$futureReminders = $user->futureReminders;

// Get sent reminders
$sentReminders = $user->sentReminders;

// Check if user has pending reminders
if ($user->hasPendingReminders()) {
    // ...
}

// Remove reminders for a specific source
$user->removeReminder($task);

// Remove all pending reminders
$user->removePendingReminders();

use Illuminate\Support\Facades\Schedule;

Schedule::command('reminders:send')->everyFiveMinutes();

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune')->daily();

$user->remindAt(
    notification: CustomNotification::class,
    source: $task,
    sendAt: now()->addHours(2),
    data: [
        'priority' => 'high',
        'category' => 'urgent',
        'custom_field' => 'value',
    ]
);

class CustomNotification extends Notification
{
    public function __construct(
        public Model $source,
        public array $data
    ) {}
    
    public function toMail($notifiable)
    {
        $priority = $this->data['priority'] ?? 'normal';
        
        return (new MailMessage)
            ->subject("Task Reminder - Priority: {$priority}")
            ->line($this->data['custom_field']);
    }
}

class SimpleNotification extends Notification
{
    public function __construct(
        public Model $source
    ) {}
}

// Usage
$user->remindAt(
    notification: SimpleNotification::class,
    source: $task,
    sendAt: now()->addDay()
);

class DataNotification extends Notification
{
    public function __construct(
        public Model $source,
        public array $data
    ) {}
}

// Usage
$user->remindAt(
    notification: DataNotification::class,
    source: $task,
    sendAt: now()->addDay(),
    data: ['priority' => 'high']
);

class DataOnlyNotification extends Notification
{
    public function __construct(
        public array $data
    ) {}
}

// Usage
$user->remindAt(
    notification: DataOnlyNotification::class,
    sendAt: now()->addDay(),
    data: ['message' => 'Custom reminder']
);

class StaticNotification extends Notification
{
    // No constructor needed
    
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('Static reminder message');
    }
}

// Usage
$user->remindAt(
    notification: StaticNotification::class,
    sendAt: now()->addDay()
);

class CustomNotification extends Notification
{
    public function __construct(
        public string $title,
        public int $priority = 1
    ) {}
}

// Usage - Custom parameters are passed via data array
$user->remindAt(
    notification: CustomNotification::class,
    sendAt: now()->addDay(),
    data: [
        'title' => 'Important Task',
        'priority' => 5
    ]
);

use AAEngineering\ScheduledReminders\Models\Reminder;

// Find all pending reminders across the system
$pending = Reminder::whereNull('sent_at')
    ->where('send_at', '<=', now())
    ->get();

// Find reminders for a specific source
$taskReminders = Reminder::where('source_type', Task::class)
    ->where('source_id', $taskId)
    ->get();

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class TaskDueNotification extends Notification implements ShouldQueue
{
    // This notification will be queued when sent
}
bash
php artisan vendor:publish --tag="scheduled-reminders-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="scheduled-reminders-config"