35 / 35

Beyond the Basics· 20 min read· Lesson 35 of 35

Capstone: The Rusty Bean Café Orders App

Part 2 · Laravel Rust. Build the Rusty Bean Café ordering site end to end: menu page, validated order form, saved orders, a queued emailed receipt, background work and a test.

You made it to the finale. You started this course not knowing what a variable was, and now you will assemble a small but real web application: the Rusty Bean Café ordering site. Visitors see the menu, a signed-in customer fills in an order form, and a receipt arrives by email. Behind the scenes, a queue worker sends that email, and a background task pings the kitchen.

This capstone builds on what you already made in earlier lessons, so each step links back to the lesson that taught the idea. The snippets follow the syntax in the official Laravel Rust documentation (opens in new tab).

Note

Laravel Rust is in beta (announced December 1, 2026), so details may shift. Names such as StoreOrder, SendReceipt and OrderReceipt are our café’s own, and parts of the code (like the view templates and the kitchen address) are illustrative. When something does not match your version, trust the docs.

What you already have, and what we add

From the Data lessons, you already have:

In this capstone we add a money helper, a menu page with an order form, a fuller StoreOrder input, a queued receipt email, a background task and a test.

If you are starting from nothing, create the app first, as in Creating Your First App, then build those three tables and models:

Terminal window
curl -fsSL https://laravel.com/install.sh | sh
laravel new rusty-bean --rust
cd rusty-bean
laravel migrate

Orders belong to a user, so customers must be signed in. Authentication itself is covered in the Laravel Rust authentication guide (opens in new tab), and we do not rebuild it here.

Fill your menu_items table with the café’s menu (see the seeding guide (opens in new tab)). Prices are integer cents:

Item Cents Item Cents
Espresso 300 Tea 250
Americano 325 Hot Chocolate 375
Cappuccino 425 Croissant 325
Flat White 400 Muffin 300
Latte 450 Bagel 350
Mocha 475 Cookie 200

Step 1: Declare your modules

New files are invisible to Rust until you declare them. The structure guide’s rule is: declare each new module in src/lib.rs, and declare each file inside it in that module’s mod.rs. If you forget a mod line, Rust skips the file without a warning, and nothing in it runs.

Here is everything we add. In src/lib.rs, next to the modules your app already declares, add:

src/lib.rs
pub mod jobs;
pub mod mail;
pub mod money;

They are pub because our tests reach them as app::jobs::SendReceipt. Now declare the files inside each new module:

src/jobs/mod.rs
mod send_receipt;
pub use send_receipt::SendReceipt;
src/mail/mod.rs
mod order_receipt;
pub use order_receipt::OrderReceipt;

src/money.rs is a single file, so pub mod money; is all it needs. The handler files from Routing and Handlers need their lines too (you may already have some):

src/http/handlers/mod.rs
pub mod menu;
pub mod orders;

And the route file imports them and registers two routes:

src/routes/web.rs
use laravel::prelude::*;
use crate::http::handlers::{menu, orders};
pub fn routes(route: &mut Router) {
route.get("/menu", menu::index);
route.post("/orders", orders::store);
}

(Keep any other routes you already have in that function.)

Step 2: A money helper

Prices stay as integer cents until the last moment. This small plain-Rust function, from Data Types and Functions, turns cents into text:

src/money.rs
pub fn dollars(cents: i64) -> String {
format!("${}.{:02}", cents / 100, cents % 100)
}

dollars(450) gives "$4.50", and {:02} pads the cents to two digits, so 5 becomes 05. (The models use i64 for integer columns, which is why this takes i64 rather than the u32 from plain Rust.)

Step 3: The menu page

Now the handler, using Querying Models and views:

src/http/handlers/menu.rs
use laravel::prelude::*;
use laravel::view::View;
use serde::Serialize;
use crate::models::MenuItem;
use crate::money::dollars;
#[derive(Serialize)]
struct MenuRow {
id: i64,
name: String,
price: String,
}
pub async fn index() -> laravel::Result<View> {
let items: Vec<MenuRow> = MenuItem::all()
.await?
.into_iter()
.map(|item| MenuRow {
id: item.id,
name: item.name,
price: dollars(item.price_cents),
})
.collect();
Ok(view("menu", data! { items }))
}
  1. MenuItem::all() loads every menu row.
  2. The iterator map (from Closures and Iterators) turns each row into a small display struct with a formatted price.
  3. view("menu", data! { items }) renders resources/views/menu.html.
resources/views/menu.html
<h1>Rusty Bean Café</h1>
<form action="/orders" method="POST">
{{ csrf_field() }}
<select name="menu_item_id">
{% for item in items %}
<option value="{{ item.id }}">{{ item.name }} ({{ item.price }})</option>
{% endfor %}
</select>
<input name="customer_name" placeholder="Your name">
<input name="note" placeholder="Anything we should know?">
<input name="quantity" type="number" value="1">
<button>Order</button>
</form>

{{ csrf_field() }} adds the hidden security token Laravel checks on form posts.

Step 4: The validated order form

This is Typed Validation and Forms in action. StoreOrder replaces the smaller PlaceOrder:

src/http/handlers/orders.rs
use laravel::model::create;
use laravel::prelude::*;
use crate::models::{MenuItem, Order, OrderItem, User};
#[laravel::input(fills = Order)]
#[validate(authenticated)]
pub struct StoreOrder {
#[validate(required, max_length(100))]
pub customer_name: String,
#[validate(max_length(500))]
pub note: Option<String>,
#[fill(skip)]
#[validate(required, exists(MenuItem::id))]
pub menu_item_id: i64,
#[fill(skip)]
#[validate(required, min_value(1), max_value(20))]
pub quantity: i64,
}
  1. #[validate(authenticated)] says any logged-in user may submit. Guests are sent to the login page.
  2. fills = Order makes it a fill struct: customer_name and note fill those Order columns.
  3. menu_item_id and quantity belong to the order item, not the order, so #[fill(skip)] keeps them out of the order write. We read them ourselves.
  4. exists(MenuItem::id) rejects an item that is not on the menu, and max_value(20) stops an order for 500 croissants.

Step 5: The queued receipt

Mailables live in the src/mail/ module we declared. Create the email struct (see Mail, Broadcasting and Inertia):

src/mail/order_receipt.rs
use laravel::mail::Mailable;
use crate::models::Order;
#[derive(Mailable)]
#[mail(subject = "Your Rusty Bean receipt")]
pub struct OrderReceipt {
pub order: Order,
pub total: String,
}

Its view, resources/views/mail/order-receipt.html, can say Thanks, {{ order.customer_name }}! Your total is {{ total }}. To email a User directly, implement From<&User> for Address once, as the mail guide shows:

src/models/user.rs
use laravel::mail::Address;
impl From<&User> for Address {
fn from(user: &User) -> Self {
Address::new(&user.email).with_name(&user.name)
}
}

Now the job from Queued Jobs. It follows the mail guide’s own pattern: store the ID, load the order with its user, then send:

src/jobs/send_receipt.rs
use laravel::mail;
use laravel::queue::{Queueable, job};
use crate::mail::OrderReceipt;
use crate::models::Order;
use crate::money::dollars;
#[derive(Queueable)]
pub struct SendReceipt {
pub order_id: i64,
}
#[job(tries = 3)]
impl SendReceipt {
async fn handle(self) -> laravel::Result<()> {
let order = Order::query()
.with(Order::user)
.find_or_fail(self.order_id)
.await?;
let total = dollars(order.total_cents);
mail::to(&order.user)
.send(OrderReceipt { order, total })
.await?;
Ok(())
}
}

The job uses send because the job itself already retries (tries = 3), as the mail guide recommends. .with(Order::user) loads the relationship up front, because relationships are never lazy-loaded.

Step 6: Finish the handler, plus a background task

Add the store handler below StoreOrder:

src/http/handlers/orders.rs
use laravel::http::client;
use laravel::task;
use serde_json::json;
use crate::jobs::SendReceipt;
pub async fn store(
Authenticated(user): Authenticated<User>,
Valid(input): Valid<StoreOrder>,
) -> laravel::Result<Redirect> {
let item = MenuItem::find_or_fail(input.menu_item_id).await?;
let total_cents = item.price_cents * input.quantity;
let order = create!(Order { user: &user, total_cents, ..&input }).await?;
create!(OrderItem {
order: &order,
menu_item: &item,
quantity: input.quantity,
unit_price_cents: item.price_cents,
})
.await?;
let order_id = order.id;
SendReceipt { order_id }.dispatch().await?;
task::background(async move {
client::post("https://kitchen.example.com/tickets")
.json(json!({ "order_id": order_id }))
.ensure_successful()
.await?;
Ok(())
})?;
Ok(Redirect::to("/menu").with("status", "Thanks! Your receipt is on its way."))
}
  1. Valid(input) is the last argument, and the handler only runs for a signed-in customer with a valid order.
  2. We look up the menu item and work out the total in cents.
  3. ..&input borrows the input, so we can still read input.quantity afterwards. user: &user sets user_id, and total_cents is set explicitly before the spread.
  4. The OrderItem links the order and the menu item, and records the price at order time.
  5. The receipt is important, so it goes on the queue. The kitchen ping is lightweight and fine to lose, so it uses task::background from Async Tasks and Concurrency. (kitchen.example.com is a made-up address.)

Tip

Want a stronger follow-up? Replace the background task with a workflow from Durable Workflows that sleeps for a day and then asks for feedback.

Step 7: A test

From Testing Your App. The test acts as a signed-in customer, fakes the queue and the HTTP client, and checks both the database and the queue:

tests/orders.rs
use app::jobs::SendReceipt;
use app::models::{MenuItem, User};
use laravel::http::client;
use laravel::model::create;
use laravel::queue;
use laravel::testing::prelude::*;
#[laravel::test]
async fn ordering_queues_a_receipt(app: Application) -> laravel::Result<()> {
let user = create!(User {
name: "Mina",
email: "mina@example.com",
password: "password",
})
.await?;
let latte = create!(MenuItem { name: "Latte", price_cents: 450 }).await?;
let latte_id = latte.id.to_string();
let queue = queue::fake();
let _http = client::fake();
app.post("/orders")
.acting_as(&user)
.form([
("customer_name", "Mina"),
("menu_item_id", latte_id.as_str()),
("quantity", "2"),
])
.await
.assert_redirect_to("/menu");
app.assert_database_count("orders", 1).await;
queue.assert_pushed::<SendReceipt>();
Ok(())
}

Run cargo test. The test only passes if the order really was saved, because a login redirect or a validation failure would not satisfy the database and queue checks. use app::jobs::SendReceipt resolves because Step 1 declared pub mod jobs;.

Run it

Terminal window
laravel migrate
laravel dev

laravel dev starts your server, a queue worker and Vite together. With the default log mailer, the receipt appears in your application log instead of being sent, which is perfect for development.

Try it yourself

  1. A customer orders 3 lattes at 450 cents each. What does dollars(...) return for the total?
  2. Add a second validation rule so that customer_name must be at least 2 characters long. Which rule, and where does it go?
Show solution
  1. 450 * 3 = 1350 cents, and dollars(1350) gives "$13.50".

  2. Add min_length(2) to the field’s rule list in StoreOrder:

#[validate(required, min_length(2), max_length(100))]
pub customer_name: String,

Recap

  • You combined the earlier migrations, models, routes, views and typed validation into a real ordering flow.
  • New modules must be declared (pub mod jobs;, mod send_receipt;, and so on), or Rust silently skips them.
  • Important work (the receipt) goes on a queue. Lightweight work (the kitchen ping) can use task::background.
  • #[laravel::test] with queue::fake() and database assertions proves the order was saved and queued without sending anything.
  • Money stayed as integer cents until the very last step.

What’s next

This is the last lesson, so there is no next lesson, only next steps. Congratulations! Here is where to keep growing:

Revisit any lesson from the beginning whenever you need a refresher. Happy building!

0 of 35 lessons complete

Search lessons

Type to search all lessons.