Laravel Eloquent local query scope reusable filters
PHP Dasun Kalupahana 77 views
<?php
// app/Models/Post.php
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\Scope;
#[Scope]
public function published(Builder $query): void
{
$query->where('status', 'published')
->whereNotNull('published_at');
}
// Usage
$posts = Post::published()->latest('published_at')->paginate(10);
Wraps a common where condition in a reusable method on the model. Use it so filters like “published” aren’t repeated across controllers.
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