Scopes & Collections in PHP
From the Laravel cheat sheet · Eloquent ORM · verified Jul 2026
Scopes & Collections
Query scopes and collection methods
php
// Local scope
class Post extends Model
{
public function scopePublished($query)
{
return $query->where('status', 'published');
}
public function scopePopular($query, $views = 100)
{
return $query->where('views', '>', $views);
}
}
// Usage
$posts = Post::published()->get();
$posts = Post::popular(500)->published()->get();💡 Scopes encapsulate common query constraints for reusability
⚡ Collections provide powerful array manipulation methods
📌 Use cursor() or chunk() for memory-efficient large datasets
🔥 Higher order messages simplify collection operations