Lesson 65 - Laravel 13 Eloquent Relationships – Part 4: Selecting Custom Columns & Optimizing Memory

Опубликовано: 26 Июль 2026
на канале: Code With ERaufi
114
4

Don’t miss out!
Buy Me a Coffee: https://buymeacoffee.com/eraufi
   • Laravel 13 Full Course – Complete Tutorial...  
Code Examples: https://github.com/ERaufi
Please like, comment, and subscribe for more tutorials, and let’s begin your journey into web development today!

In this lesson of our Laravel 13 Full Course, we focus on advanced query optimization: Selecting Custom Columns in Eloquent Relationships.

By default, when you eager load a relationship using with('user'), Laravel runs a SELECT * statement under the hood. If your related table contains dozens of columns like hashed passwords, API tokens, and timestamps, you are wasting server memory and bloating your API payloads. In this tutorial, you'll learn how to tell Laravel exactly which columns to fetch.

We will cover two main approaches using modern PHP 8.3/8.4 standards:

The Super-Clean Colon Syntax (with('relation:id,column')) for lightning-fast row reduction.

The Advanced Closure Syntax (with(['relation' = fn($q) = $q select(...)])) when you need to combine custom column selection with order conditions or where filters.

⚠️ CRITICAL TRAP ALERT: We will also break down the most common junior developer mistake that causes relationships to return empty or null data—forgetting to explicitly include the id or foreign key columns in your custom select array.

Hit that like button, subscribe to optimize your backend, and let's clean up our queries!