Notifications

Contents
Channels
Mail channel
Database channel
Notify users

Channels

One event often needs several delivery paths: mail for attention and database for history. A Notification class lists its channels in one via method.

Generate the class with artisan, then add one method per channel.

php artisan make:notification OrderShipped

INFO Notification [app/Notifications/OrderShipped.php] created successfully.

Mail channel

The mail channel builds a classic message with subject and lines. Chain short calls on the MailMessage and keep each line plain.

Test with the log driver before sending to real inboxes.

public function via($notifiable) { return ["mail"]; } public function toMail($notifiable) { return (new MailMessage)->subject("Order shipped")->line("Good news."); }

Database channel

The database channel stores a payload row for later display in the app. toArray returns the facts the bell icon needs, like the order id.

The notification stays until the user reads or deletes it.

public function via($notifiable) { return ["database"]; } public function toArray($notifiable) { return [ "order_id" => 7, ]; }

Notify users

Any model with the Notifiable trait can receive notifications. Call notify with the class and Laravel fans the message out to every listed channel.

One call reaches mail and database together, with no extra wiring.

$user->notify(new OrderShipped($order));

Next: Scheduler

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: