A relationship is a field with an attribute, not a method you call and hope the query happened. Relationships are never lazy-loaded. If the field was not loaded, reading it is an error that names the missing .with call. That is the whole point of the model-relationships guide: the N+1 query cannot hide in a loop.
Add the author beside the user_id column. The relationships guide keeps both: the integer is the foreign key, and #[belongs_to] is not a column. By convention Laravel appends _id to the relationship field name, so user reads user_id.
use laravel::model::{BelongsTo, model};
use laravel::time::Timestamp;
use crate::models::User;
#[model]
pub struct Note {
pub id: i64,
pub user_id: i64,
pub title: String,
pub body: String,
pub created_at: Timestamp,
pub updated_at: Timestamp,
#[belongs_to]
pub user: BelongsTo<User>,
}
On the starter’s User, add the inverse beside the existing fields. Do not replace the struct. The users table and the session guard already depend on it.
use laravel::model::HasMany;
use crate::models::Note;
// inside the existing User struct
#[has_many]
pub notes: HasMany<Note>,
HasMany means a user has many notes. BelongsTo means a note belongs to one user. The attribute generates the query methods. You do not write the SQL join by hand for this pair.
Ask for the author in the same query plan
let notes = Note::query()
.with(Note::user)
.latest(Note::created_at)
.limit(20)
.get()
.await?;
for note in ¬es {
println!("{} by {}", note.title, note.user.name);
}
.with(Note::user) loads the relationship. A loaded BelongsTo<User> dereferences to User, so note.user.name is the author’s name. Pass a tuple when you need more than one relationship: .with((Note::user,)) is the one-element form the guide uses for Post::user together with Post::comments.
Without .with, reading note.user.name panics. The relationships guide’s message names the fix:
Note::user isn't loaded; add `.with(Note::user)` to the query
There is no hidden query per row. is_loaded() and try_loaded() are the documented ways to look before you read.
The other direction is the same idea. find_or_fail is the guide’s lookup when the id must exist:
let user = User::query()
.with(User::notes)
.find_or_fail(user_id)
.await?;
let titles: Vec<&str> = user.notes.iter().map(|note| note.title.as_str()).collect();
A loaded HasMany<Note> dereferences to the notes. Route model binding loads the model, not its relationships. A handler show(mut note: Note) that reads note.user still needs note.load(Note::user).await? before the field. If you only need notes for one user id, filter Note::user_id instead of loading the user and then the collection. The policy in the next course compares user.id with note.user_id, which is why the column stays on the struct.
Creating a note can set the relationship with a reference, which is what the models guide shows for create!:
let note = create!(Note {
title: "Loaded on purpose",
body: "The author came from .with.",
user: &user,
}).await?;
This still belongs on a temporary route. The next course replaces it with a validated form that sets user from the authenticated account, not from the request body.
Check your work
List notes with .with(Note::user). Open one note without .with and record the panic that names the missing load.