I’m always excited to take on new projects and collaborate with innovative teams to build impactful digital solutions.
Go beyond the basics of Laravel Eloquent with 20 advanced query techniques including eager loading, scopes, subqueries, unions, and performance optimization patterns.
Eloquent ORM is Laravel's elegant ActiveRecord implementation. While most developers know the basics, mastering advanced query techniques separates good developers from great ones. This guide covers 20 advanced techniques that will make your queries faster, cleaner, and more maintainable.
The N+1 problem is the most common performance issue in Laravel applications:
// Bad: N+1 queries
$posts = Post::all();
foreach ($posts as $post) {
echo $post->user->name; // Query per post!
}
// Good: Eager loaded
$posts = Post::with('user')->get();
$books = Book::with('author.contacts')->get();
// Load multiple relationships
$posts = Post::with(['author', 'comments' => function ($query) {
$query->where('approved', true);
}])->get();
$users = User::with(['posts' => function ($query) {
$query->where('published', true)->orderBy('created_at', 'desc');
}])->get();
$books = Book::all();
if ($someCondition) {
$books->load('author', 'publisher');
}
$users = User::with('posts:id,user_id,title')->get();
class Post extends Model
{
public function scopePublished($query)
{
return $query->where('status', 'published');
}
public function scopeOfAuthor($query, $authorId)
{
return $query->where('author_id', $authorId);
}
}
// Usage
$posts = Post::published()->ofAuthor(5)->get();
public function scopeWhereStatus($query, $status)
{
return $query->where('status', $status);
}
$posts = Post::whereStatus('draft')->get();
use Illuminate\Support\Facades\DB;
$users = User::query()
->addSelect(['last_post_at' => Post::select('created_at')
->whereColumn('posts.user_id', 'users.id')
->orderByDesc('created_at')
->limit(1)
])
->get();
use Illuminate\Support\Facades\DB;
$users = User::query()
->orderByDesc(Post::select('created_at')
->whereColumn('posts.user_id', 'users.id')
->orderByDesc('created_at')
->limit(1))
->get();
$query = User::query();
$query->when($request->filled('role'), function ($q) use ($request) {
return $q->where('role', $request->role);
})->when($request->filled('active'), function ($q) use ($request) {
return $q->where('active', $request->boolean('active'));
});
$users = $query->get();
// Posts with at least 3 approved comments
$posts = Post::whereHas('comments', function ($query) {
$query->where('approved', true);
}, '>=', 3)->get();
// Users who never logged in
$users = User::whereDoesntHave('logins')->get();
$orders = Order::query()
->selectRaw('DATE(created_at) as date, COUNT(*) as total, SUM(total_amount) as revenue')
->groupByRaw('DATE(created_at)')
->orderByDesc('date')
->get();
$users = User::select('id', 'name')
->selectRaw('CASE WHEN active = 1 THEN "Active" ELSE "Inactive" END as status')
->get();
$first = User::where('role', 'admin');
$second = User::where('role', 'moderator');
$users = $first->union($second)->get();
User::where('active', true)->chunk(500, function ($users) {
foreach ($users as $user) {
// Process each user in chunks of 500
}
});
// Chunk by ID for very large tables
User::where('active', true)->chunkById(500, function ($users) {
// ...
});
foreach (User::cursor() as $user) {
// Stream results one at a time, low memory footprint
}
User::query()->lazy(200)->each(function ($user) {
// Process lazily
});
Flight::upsert([
['destination' => 'Tokyo', 'price' => 1500],
['destination' => 'Osaka', 'price' => 1200],
], ['destination'], ['price']);
DB::transaction(function () {
$order = Order::create([...]);
$order->items()->createMany([...]);
Payment::create([...]);
}, 5); // 5 retry attempts
// Manual control
DB::beginTransaction();
try {
// ...
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
throw $e;
}
class PostObserver
{
public function created(Post $post): void
{
Log::info('New post created: ' . $post->title);
Notification::send(Subscriber::all(), new NewPostNotification($post));
}
}
// Register in AppServiceProvider
Post::observe(PostObserver::class);
These 20 techniques cover the essential advanced Eloquent patterns. Master them and your queries will be faster, your code cleaner, and your application ready for production scale.
Your email address will not be published. Required fields are marked *