Skip to content

Lesson 6 of 22 · Queries and Data Access

Laravel Select and Pluck Instead of Every Column

Intermediate•
lesson•
November 30, 2026•
~4 min read

The arrivals query is in good shape after the earlier fixes: relations loaded at the query site with with(['guest', 'room']), the paid total computed by the database with withSum(). Look at what it actually sends over the wire:

SQL
select `reservations`.*, (select sum(`payments`.`amount_cents`) from `payments` where `reservations`.`id` = `payments`.`reservation_id`) as `paid_cents` from `reservations` where date(`check_in`) = ? order by `check_in` asc select * from `guests` where `guests`.`id` in (12, 31, 47, ...) select * from `rooms` where `rooms`.`id` in (3, 8, 14, ...)

reservations.*, guests.*, rooms.*. The template reads the guest's name, the room's number, and two cent amounts. The query fetches every column of three tables.

Why it was fine and why it stopped being fine

SELECT * is Eloquent's default. get(), first(), find(), and all() all ask for every column unless you say otherwise. On a young table this costs nothing you can measure: reservations started with ids, dates, counts, a status, and a total. Pulling all of it to show a name and a room number was wasteful and cheap.

The table did not stay young. The front desk asked for a notes text column, and it holds paragraphs about late arrivals and cot requests. A special_requests JSON column holds the guest's answers from the booking form, a few kilobytes per reservation. The guests table grew a preferences JSON column. None of that appears on the arrivals list, and all of it is fetched, transferred, and hydrated on every load of the dashboard.

Eloquent does not only move the bytes. It builds a model per row and fills every attribute, so the kilobytes in special_requests are copied into a model for every arrival and thrown away when the request ends. The dashboard got heavier without a line of its code changing, and the query log shows the same query it always showed.

Name the columns

Ask for what the screen reads. On the main query that is select(). On the eager loads it is the relation:column,column syntax.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Models\Reservation; use App\Reports\OccupancyReport; use Illuminate\Support\Carbon; use Illuminate\View\View; class DashboardController extends Controller { public function __construct(private OccupancyReport $occupancy) { } public function __invoke(): View { $today = Carbon::today(); $arrivals = Reservation::query() ->select(['id', 'guest_id', 'room_id', 'total_cents']) ->whereDate('check_in', $today) ->with(['guest:id,name', 'room:id,number']) ->withSum('payments as paid_cents', 'amount_cents') ->orderBy('check_in') ->get(); return view('dashboard', [ 'arrivals' => $arrivals, 'occupancy' => $this->occupancy->forMonth($today->year, $today->month), ]); } }
SQL
select `id`, `guest_id`, `room_id`, `total_cents`, (select sum(`payments`.`amount_cents`) from `payments` where `reservations`.`id` = `payments`.`reservation_id`) as `paid_cents` from `reservations` where date(`check_in`) = ? order by `check_in` asc select `id`, `name` from `guests` where `guests`.`id` in (12, 31, 47, ...) select `id`, `number` from `rooms` where `rooms`.`id` in (3, 8, 14, ...)

The models still behave like models. $reservation->guest->name still works, outstandingCents() still reads total_cents and paid_cents. They carry five attributes instead of the whole row, and adding a column to reservations changes nothing about this page.

Two rules keep this from biting back. The foreign keys go in the select: guest_id and room_id are how Eloquent matches the eager-loaded guests and rooms back to their reservations, and without them every relation comes back empty, or throws under strict mode. And the eager load needs the related table's primary key for the same reason, which is why it is guest:id,name and not guest:name.

The dropdown case

The reservation form has a room dropdown. It needs a room number per id and nothing else, and it does not need models at all.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Models\Room; use Illuminate\View\View; class ReservationController extends Controller { public function create(): View { $rooms = Room::query() ->orderBy('number') ->pluck('number', 'id'); return view('reservations.create', ['rooms' => $rooms]); } }

The other actions are unchanged.

pluck('number', 'id') sends select number, id from rooms order by number and returns a plain keyed collection. No hydration, and no columns the dropdown never shows.

Where SELECT * is still fine

A single-row lookup that goes on to change the row wants every column. Reservation::query()->findOrFail($id) before a save() should stay as it is, because a partial model is missing the columns that observers, accessors and the rest of the use case may read, and with strict mode on, reading one throws MissingAttributeException. Name columns on list queries and read-only lookups, where the row count multiplies the width, and leave the default alone on the row a use case is about to change.

The one thing to remember

On a list query, the cost is rows times columns, and the column count is the one that grows without anyone touching the query. Select what the screen reads, foreign keys included.

Course Content