<?php
namespace App\Notifications;
use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
class PostPublished extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
public Post $post
) {}
public function via($notifiable): array
{
$channels = ['database'];
if ($notifiable->notificationPreferences->email) {
$channels[] = 'mail';
}
if ($notifiable->notificationPreferences->sms) {
$channels[] = 'vonage'; // SMS via Vonage
}
return $channels;
}
public function toMail($notifiable): MailMessage
{
return (new MailMessage)
->subject('New Post: ' . $this->post->title)
->greeting('Hello!')
->line($this->post->author->name . ' published a new post.')
->action('Read Post', route('posts.show', $this->post))
->line('Thank you for being a subscriber!');
}
public function toDatabase($notifiable): array
{
return [
'post_id' => $this->post->id,
'title' => $this->post->title,
'author' => $this->post->author->name,
'url' => route('posts.show', $this->post),
];
}
public function toSlack($notifiable): SlackMessage
{
return (new SlackMessage)
->content('New post published!')
->attachment(function ($attachment) {
$attachment
->title($this->post->title, route('posts.show', $this->post))
->fields([
'Author' => $this->post->author->name,
'Published' => $this->post->published_at->diffForHumans(),
]);
});
}
}
<?php
// Notify a single user
$user->notify(new PostPublished($post));
// Notify multiple users
$followers = $post->author->followers;
Notification::send($followers, new PostPublished($post));
// On-demand notification (without Notifiable model)
Notification::route('mail', 'admin@example.com')
->route('slack', config('services.slack.webhook'))
->notify(new PostPublished($post));
// Get user's notifications
$notifications = $user->notifications;
$unread = $user->unreadNotifications;
// Mark as read
$user->unreadNotifications->markAsRead();
// Delete notification
$notification->delete();
Laravel notifications send messages across email, SMS, Slack, database, and more via a unified API. I create notification classes extending Notification with channel-specific methods—toMail(), toDatabase(), toSlack(). The via() method determines which channels to use based on user preferences. Notifications queue automatically with ShouldQueue. Database notifications store in a notifications table for in-app messages. The Notifiable trait on users provides notify() and notifyNow() methods. On-demand notifications send to ad-hoc recipients. Notification events enable logging and analytics. This abstraction makes cross-channel messaging consistent and maintainable without vendor-specific code scattered everywhere.
Related snips
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'noreply@example.com'
def welcome_email(user)
@user = user
ActionMailer advanced patterns for transactional emails
<?php
namespace App\Providers;
use App\Contracts\PaymentGateway;
use App\Services\StripePaymentGateway;
Laravel service container and dependency injection
module EmailNormalization
extend ActiveSupport::Concern
included do
attr_accessor :soft_warnings
Soft Validation: Normalize + Validate Email
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
Laravel mix/Vite for asset compilation
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Laravel database migrations for schema management
package com.example.myapp.utils
import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.app.PendingIntent
Notification channels and categories
Share this code
Here's the card — post it anywhere.