Installation
composer require atakajlo/notifications
Publish the config (if available):
php artisan vendor:publish --provider="Atakajlo\Notifications\NotificationsServiceProvider"
Basic Setup
config/app.php (if not auto-discovered):
'providers' => [
Atakajlo\Notifications\NotificationsServiceProvider::class,
],
config/notifications.php for default configurations (e.g., channels, drivers).First Use Case: Sending a Notification
use Atakajlo\Notifications\Notification;
use Atakajlo\Notifications\Channels\MailChannel;
// Define a notification class
class OrderShipped extends Notification
{
public function via($notifiable)
{
return [MailChannel::class];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Your order has shipped!')
->line('Thank you for your purchase.');
}
}
// Send the notification
$user = User::find(1);
$user->notify(new OrderShipped());
Channel-Specific Notifications
Notification and define via() to specify channels (e.g., MailChannel, DatabaseChannel, SlackChannel).public function via($notifiable)
{
return [new SMSChannel('twilio')];
}
Dynamic Recipients
notify() with collections or multiple recipients:
$users = User::where('role', 'admin')->get();
$users->notify(new SystemUpdate());
Queued Notifications
$user->notify(new OrderShipped())->onQueue('notifications');
Customizing Notifications
toArray(), toMail(), or other channel methods per notification type.public function toCustomChannel($notifiable)
{
return ['key' => 'value'];
}
Event-Based Triggers
OrderShipped event):
event(new OrderShipped($order));
// In the event listener:
$order->user->notify(new OrderShippedNotification($order));
Notification::fake() for unit tests:
Notification::fake();
$user->notify(new OrderShipped());
Notification::assertSentTo($user, OrderShipped::class);
$user->notify(new OrderShipped())->locale('es');
Channel Configuration
mail, database) are properly configured in config/services.php or config/notifications.php.'mail' => [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
// ... other SMTP settings
],
Missing notifiable Interface
Illuminate\Notifications\Notifiable to receive notifications.use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
}
Queue Stuck Jobs
php artisan queue:work).failed_jobs table (if using database queue).storage/logs/laravel.log).Channel-Specific Quirks
from address is set in config/mail.php.notifications table exists (migrate if needed).\Log::debug('Notification sent', ['data' => $notification->toArray()]);
failed_jobs table or run:
php artisan queue:failed
notifications table directly.Custom Channels
Illuminate\Notifications\ChannelManager or Atakajlo\Notifications\Channels\BaseChannel.namespace App\Notifications\Channels;
use Atakajlo\Notifications\Channels\BaseChannel;
class PushChannel extends BaseChannel
{
public function send($notifiable, $notification)
{
// Custom logic (e.g., Firebase Cloud Messaging)
}
}
Notification Middleware
app/Providers/EventServiceProvider:
protected $listen = [
'Atakajlo\Notifications\Events\NotificationPrepared' => [
\App\Notifications\Middleware\LogNotification::class,
],
];
Dynamic Channel Selection
via() to conditionally select channels:
public function via($notifiable)
{
return $notifiable->prefers_sms ? [SMSChannel::class] : [MailChannel::class];
}
Batch Processing
batch() helper or chunk processing:
User::chunk(100, function ($users) {
$users->each->notify(new WeeklyNewsletter());
});
How can I help you explore Laravel packages today?