Laravel Events and Notifications Example
Learn how to create and handle events in Laravel to send notifications with this snippet. Try it in our PHP Sandbox!
edit_note
PHP Code Editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// Description: This snippet demonstrates creating and handling an event in Laravel to send a notification.
// Task: Trigger a notification when a post is created.
// Requirements: Laravel installed, runs in a Tinker-like environment.
// Run this in sandbox.ws to test.
// SEO Title: Laravel Events and Notifications Example | Sandbox.ws
// SEO Description: Learn how to create and handle events in Laravel to send notifications with this snippet. Try it
in our PHP Sandbox!
// SEO Keywords: Laravel events, Laravel notifications, Laravel event listener, PHP sandbox
// Include necessary classes
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
// Set up in-memory SQLite database
\Illuminate\Support\Facades\DB::connection()->getPdo()->exec('PRAGMA foreign_keys = ON;');
// Create schema for posts
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
terminal
Execution Result
play_circle_outline
Ready to execute
Click the "Run Script" button to see the output here
article
Description
Discover how to use Laravel’s powerful event and listener system to trigger actions automatically when a post is created. In this example, you’ll learn how to define a custom event (PostCreated), a corresponding listener (SendPostCreatedNotification), and link them together to send a notification every time a new post is added to the database.
This lightweight snippet uses an in-memory SQLite database and can be tested instantly in https://sandbox.ws — no setup required. It’s ideal for understanding the fundamentals of Laravel events, event broadcasting, and notifications in modern PHP applications.
Comments
No comments yet
Be the first to share your thoughts!