All posts

Laravel Rust models: the ORM that refuses to hide your N+1

Published December 2, 2026 · by Majd Ghithan, written with the help of AI

Laravel Rust models: the ORM that refuses to hide your N+1

The worst performance bugs I've dealt with in Laravel never threw an error. A page worked fine with 20 rows in staging, then took seconds in production because a loop in a Blade view quietly ran one query per row. N+1 is the classic Laravel scaling bug, and the reason it survives code review is that lazy loading makes it invisible.

Laravel Rust makes one decision about this that I think matters more than any benchmark: "Relationships are never lazy-loaded behind the scenes." Here's how models work there, what that rule actually does, and what is checked at compile time versus at runtime. That last distinction matters, and I've already seen it blurred.

As with everything in this series: Laravel Rust is beta (0.1.x), and APIs can change before 1.0.

A model is a struct

From the models docs: "A model is a Rust struct annotated with #[model], where each field maps to a column and each instance represents a row." The macro also derives Clone, Debug and Serialize for you.

Here's the Post model from the relationships docs:

// src/models/post.rs
use laravel::model::{BelongsTo, HasMany, model};
use laravel::time::Timestamp;

use crate::models::{Comment, User};

#[model]
pub struct Post {
    pub id: i64,
    pub user_id: i64,
    pub title: String,
    #[default]
    pub votes: i64,
    pub published_at: Option<Timestamp>,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,

    #[belongs_to]
    pub user: BelongsTo<User>,
    #[has_many]
    pub comments: HasMany<Comment>,
}

If you know Eloquent, you'll read this immediately. The difference is that every column is written down with its type. In Eloquent, columns are whatever the database returns, and $post->vots (typo) is just null. Here, the struct is the contract.

Writes go through the create! and update! macros. Relationships are fields marked #[belongs_to] or #[has_many].

Eager loading is the only loading

You load relationships with .with(...) on the query:

let posts = Post::query().with(Post::user).get().await?;

Several at once take a tuple: .with((Post::user, Post::comments)). Nested loads look like this: .with(User::posts.with((Post::comments, Post::tags))). Notice the relationship is Post::user, not the string 'user'. That's what lets the compiler check it.

Here's the same intent in both frameworks:

Laravel PHP - works, quietly runs N+1
$posts = Post::all();

foreach ($posts as $post) {
    echo $post->user->name;
    // one extra query per post
}
Laravel Rust - you load it, or it panics
let posts = Post::query()
    .with(Post::user)
    .get()
    .await?;

The PHP version is valid code. It passes tests. It ships. It is also exactly the kind of loop I've seen slow production pages down once the table grows. In Laravel PHP you can fix it with Post::with('user'), but nothing forces you to.

What happens when you forget

This is the part to get exactly right, because it's easy to overstate.

Forgetting .with(...) is not a compile error. It's a runtime panic. The docs say: "Accessing a relationship that has not been loaded panics with a message naming the missing eager load":

Post::comments isn't loaded; add `.with(Post::comments)` to the query

So the bug is still possible. What changes is how it behaves. In PHP, the forgotten eager load costs you one query per row, silently, forever. In Laravel Rust, it fails the first time that code path runs, and the error message tells you the fix. If your tests or your local run hit that path, you find it before production does.

When a relationship might legitimately not be loaded, there are safe checks: is_loaded() and try_loaded(), which returns an Option<&T> instead of panicking.

What the compiler does catch

The compile-time part is about names and types. The docs' example: comparing Post::votes against a string "fails at compile time rather than failing at runtime." Columns and relationships are referenced as Post::votes and Post::user, not strings, so a typo or a renamed column breaks the build instead of a page.

If you've ever renamed a column in a migration and then grepped the codebase for every where('old_name', ...), you know why this is nice. The compiler does that grep for you, and it doesn't miss the one inside a scope three files away.

Why this matters at scale

On the dashboards I've built that served 100K+ users, N+1 was never one dramatic bug. It was many small ones, each added by a reasonable developer, each harmless with test data. A relationship accessed in a new Blade partial. A new column in an API resource that touches a relation. Each review looked fine, because lazy loading hides the query.

At scale the cost isn't the single slow page. It's the background noise: more queries per request, a database that's busier than it should be, and a team that has to keep N+1 in their heads during every review.

A rule that makes the missing eager load fail loudly moves that work from human attention to the framework. That's the kind of guardrail I care about as a tech lead. Not because my team is careless, but because a guardrail works on a Friday afternoon when people are tired.

My take

I won't pretend this makes N+1 impossible. It doesn't: the panic happens at runtime, and an untested path can still reach production. But "fails loudly with the fix in the message" is a big step up from "works and costs you quietly". Combined with compile-time checked columns, it's the most convincing part of Laravel Rust for me, more than the 18x headline. Even if you stay on PHP, the lesson carries over: treat lazy loading as a bug to catch in development, not a convenience.

🤖 Heads up: this post was drafted with AI. Spot something wrong or off?Email me the correction →
Majd Ghithan

Majd Ghithan

Full-Stack Engineer & Tech Lead

More posts

Laravel in PracticeDec 2, 20266 min read

Laravel Go is here. What it is, how it differs from Laravel Rust, and where Goravel stands

On December 1, Laravel released the first beta of Laravel Rust. One day later, on December 2, Taylor Otwell published the first beta of Laravel Go. Two new official Laravel fram…

Laravel in PracticeDec 2, 20265 min read

The same Laravel ideas in three languages: PHP, Rust, and Go

The fastest way to understand a framework is to look at code you already know written in it. So instead of another feature list, this article puts the same Laravel concepts next…

Beyond the CodeDec 2, 20266 min read

Laravel PHP, Laravel Rust, or Goravel? Picking by fit, not by hype

As of December 2026 there are three ways to write an app with Laravel's ideas: the original in PHP, the new Laravel Rust beta, and Goravel in Go. They share a philosophy. They d…

Laravel in PracticeDec 2, 20265 min read

What is Goravel? Laravel's ideas, written in Go

Laravel Rust went into beta on December 1, 2026. It is the first time the Laravel team itself has taken the framework's conventions into a compiled language. It is not the first…

Beyond the CodeDec 2, 20264 min read

Laravel Rust is out. Should your team switch?

A week after the Laravel Rust beta landed on December 1, the question I keep getting is the same one: "Should we move our app to Rust?" For almost every team asking, my answer i…

Beyond the CodeDec 2, 20264 min read

Every new Laravel Rust app ships instructions for your AI agent

When I create a new Laravel project, one of the first things I do is add instructions for the coding agent: how this codebase is structured, which conventions to follow, which m…