Write Reports With the Query Builder, Not Eloquent Loops
A monthly occupancy table needs two numbers per room type: how many stays and how much revenue. The Eloquent version loads every confirmed reservation in the month, then the room and room type behind each one, builds a model for every row, and adds them up in a foreach. The table fits on an index card, and the work to produce it grows with every stay the property takes.
Don't do this
<?php
declare(strict_types=1);
namespace App\Reports;
use App\Models\Reservation;
use Carbon\CarbonImmutable;
final readonly class OccupancyReport
{
/**
* @return array<string, array{stays: int, revenue_cents: int}>
*/
public function between(CarbonImmutable $from, CarbonImmutable $to): array
{
$reservations = Reservation::query()
->where('status', 'confirmed')
->whereBetween('check_in', [$from->toDateString(), $to->toDateString()])
->with('room.roomType')
->get();
$report = [];
foreach ($reservations as $reservation) {
$type = $reservation->room->roomType->name;
$report[$type]['stays'] = ($report[$type]['stays'] ?? 0) + 1;
$report[$type]['revenue_cents'] = ($report[$type]['revenue_cents'] ?? 0) + $reservation->total_cents;
}
ksort($report);
return $report;
}
}Do this instead
A small readonly class gives the three values names and types.
<?php
declare(strict_types=1);
namespace App\Reports;
final readonly class OccupancyRow
{
public function __construct(
public string $roomType,
public int $stays,
public int $revenueCents,
) {
}
}The report asks the database to group and sum.
<?php
declare(strict_types=1);
namespace App\Reports;
use Carbon\CarbonImmutable;
use Illuminate\Database\ConnectionInterface;
final readonly class OccupancyReport
{
public function __construct(private ConnectionInterface $connection)
{
}
/**
* @return list<OccupancyRow>
*/
public function between(CarbonImmutable $from, CarbonImmutable $to): array
{
return $this->connection->table('reservations')
->join('rooms', 'rooms.id', '=', 'reservations.room_id')
->join('room_types', 'room_types.id', '=', 'rooms.room_type_id')
->where('reservations.status', 'confirmed')
->whereBetween('reservations.check_in', [$from->toDateString(), $to->toDateString()])
->groupBy('room_types.name')
->orderBy('room_types.name')
->selectRaw('room_types.name as room_type, count(*) as stays, sum(reservations.total_cents) as revenue_cents')
->get()
->map(fn (object $row): OccupancyRow => new OccupancyRow(
roomType: (string) $row->room_type,
stays: (int) $row->stays,
revenueCents: (int) $row->revenue_cents,
))
->all();
}
}One query returns one row per room type, already summed, and no Reservation, Room, or RoomType model is built. ConnectionInterface is injected in place of the DB facade, and in Laravel 13 the container resolves that type-hint to the default connection.
count, sum, and group by run unchanged on MySQL, Postgres, and SQLite. Date arithmetic such as DATEDIFF does not, so check the dialect before adding it. Dropping to the query builder gives up casts, accessors, and relations, and a report uses none of them. Keep Eloquent for the rows a use case changes.