PHP
Implementing Eloquent Global Scopes for Automatic Query Filtering
Apply application-wide query constraints automatically to Eloquent models using global scopes, ensuring consistent filtering across many queries.
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class PublishedScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('published', true);
}
}
// --- In your Model (e.g., Post.php) ---
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\PublishedScope;
class Post extends Model
{
protected static function booted()
{
static::addGlobalScope(new PublishedScope);
// You can also add anonymous global scopes
static::addGlobalScope('recent', function (Builder $builder) {
$builder->where('created_at', '>', now()->subMonths(3));
});
}
// ...
}
// --- Usage Examples ---
// Only published and recent posts will be retrieved by default
$posts = Post::all(); // Applies PublishedScope and 'recent' scope automatically
// Get all posts, including unpublished ones, by removing a specific global scope
$allPosts = Post::withoutGlobalScope(PublishedScope::class)->get();
// Get all posts, including unpublished and non-recent, by removing all global scopes
$rawPosts = Post::withoutGlobalScopes()->get();
// Remove an anonymous global scope
$postsNotLimitedByRecent = Post::withoutGlobalScope('recent')->get();
How it works: This snippet demonstrates how to implement and use Eloquent global scopes. Global scopes allow you to add constraints to all queries for a given model automatically. By defining a class that implements `Illuminate\Database\Eloquent\Scope` and registering it in your model's `booted` method, you can ensure consistent filtering (e.g., only showing published posts). You can also define anonymous global scopes. Use `withoutGlobalScope()` or `withoutGlobalScopes()` to temporarily remove these constraints for specific queries.