Installation:
composer require moox/notifications
php artisan mooxnotifications:install
First Use Case:
use Moox\Notifications\Notification;
use Moox\Notifications\Channels\EmailChannel;
$notification = new Notification('Welcome!');
$notification->to(new EmailChannel('user@example.com'))
->setContent('Welcome to our platform!');
$notification->send();
Where to Look First:
config/notifications.php (published via vendor:publish).app/Notifications/Channels/ (auto-discovered).database/migrations/ for notification-related tables.Defining Notifications:
Notification class to create reusable notification types:
class OrderConfirmation extends Notification
{
public function __construct($orderId)
{
$this->orderId = $orderId;
}
public function to($channel)
{
return $channel->setData(['order_id' => $this->orderId]);
}
}
Channel Integration:
EmailChannel with customizable templates:
$notification->to(new EmailChannel('user@example.com'))
->setTemplate('emails.order_confirmation');
WebhookChannel for custom APIs:
$notification->to(new SlackChannel('https://hooks.slack.com/...'))
->setPayload(['text' => 'New alert!']);
Batch Processing:
$users = User::where('role', 'admin')->get();
foreach ($users as $user) {
$notification->to(new EmailChannel($user->email))->send();
}
// Or use a queue for async processing:
$notification->to(new EmailChannel($user->email))->delay(60)->send();
Dynamic Content:
$notification->setData(['user' => $user, 'count' => 5])
->to(new EmailChannel($user->email));
Event-Driven Triggers:
event(new OrderPlaced($order));
// In EventServiceProvider:
OrderPlaced::listen(function ($event) {
$notification = new OrderConfirmation($event->order->id);
$notification->to(new EmailChannel($event->user->email))->send();
});
Channel Misconfiguration:
config/notifications.php under channels.dd($channel->validate()) to check channel setup.Migration Conflicts:
notifications, notification_recipients) don’t clash with existing tables.php artisan migrate:status to check for conflicts.Queue Stuck Jobs:
failed_jobs table and set up a dead-letter queue handler.Template Caching:
php artisan view:clear
LogChannel for debugging:
$notification->to(new LogChannel())->send(); // Logs to storage/logs/laravel.log
dd($notification->getPayload()) to verify data before sending.Custom Channels:
Moox\Notifications\Channels\BaseChannel:
class CustomChannel extends BaseChannel
{
public function send($notifiable, $notification)
{
// Custom logic (e.g., SMS API call)
}
}
config/notifications.php:
'channels' => [
'custom' => \App\Notifications\Channels\CustomChannel::class,
],
Middleware:
$notification->via(new RateLimitMiddleware(5, 'minute'));
Testing:
$channel = Mockery::mock(EmailChannel::class);
$channel->shouldReceive('send')->once();
$notification->to($channel)->send();
config/notifications.php:
'default' => 'email',
max_recipients in config to prevent memory issues for bulk sends.User::chunk(100, function ($users) {
foreach ($users as $user) {
$notification->to(new EmailChannel($user->email))->send();
}
});
How can I help you explore Laravel packages today?