Laravel model observers and events with #[ObservedBy]
PHP Dasun Kalupahana 57 views
<?php
// php artisan make:observer PostObserver --model=Post
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
#[ObservedBy([PostObserver::class])]
class Post extends Model {}
class PostObserver
{
public function created(Post $post): void
{
// e.g. clear cache, send notification
}
}
Moves model lifecycle logic (created, updated, deleted) out of controllers into one class. The #[ObservedBy] attribute registers it without a service provider.
Learn more on the blog
MySQL
PostgreSQL vs MySQL Performance: Real Benchmarks Compared
Last updated: 7 November 2026. See the update log at the end."Which...
8 min read
PHP
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
9 min read
Docker
Docker Build Too Slow? 10 Ways to Speed It Up
A slow Docker build usually has one of a few causes: you...
8 min read