📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Laravel Framework Querying with Eloquent Advanced

Querying with Eloquent Advanced

6 min read
Use chunking, lazy collections, raw SQL expressions, and JSON column queries in Eloquent.

Advanced Eloquent Queries

// Chunking large datasets
Post::chunk(200, fn($posts) => $posts->each->sendNotification());

// Lazy collections — memory efficient
Post::lazy()->each(fn($post) => processPost($post));

// Raw expressions
Post::selectRaw("*, MONTH(created_at) as month")
    ->groupByRaw("MONTH(created_at)")
    ->get();

// Subqueries
$users = User::addSelect([
    "last_post_at" => Post::select("created_at")
        ->whereColumn("user_id", "users.id")
        ->latest()
        ->limit(1)
])->get();

// JSON columns
Post::where("meta->featured", true)->get();
Post::whereJsonContains("tags", "laravel")->get();

// Full text search
Post::whereFullText(["title", "body"], "Laravel tips")->get();