The Prelude
Introduction
Most files in your application use the same handful of Laravel types: requests, responses, redirects, the router, and views. Instead of importing each one, you may import Laravel's prelude at the top of the file:
1use laravel::prelude::*;23#[laravel::input]4pub struct Rename {5 name: Option<String>,6}78pub async fn store(request: Request) -> laravel::Result<Redirect> {9 let name = request.parse::<Rename>()?.name.or_abort(422)?;1011 // ...1213 Ok(Redirect::back())14}1use laravel::prelude::*;23#[laravel::input]4pub struct Rename {5 name: Option<String>,6}78pub async fn store(request: Request) -> laravel::Result<Redirect> {9 let name = request.parse::<Rename>()?.name.or_abort(422)?;1011 // ...1213 Ok(Redirect::back())14}
Most examples in these docs assume this import.
What the prelude includes
The prelude includes the types most handlers and route files use:
Request,Response,Redirect,Json,Query, andStatusCodeRouterandParamsfor your routesSession,Inject, andApplicationValidfor validated input, andAuthenticatedfor the logged-in user- The
route!andredirect!macros for route URLs and redirects - The
viewfunction anddata!macro for views - The
EnumandNewtypederives, for your own enums and single-field structs in model columns and form fields
It also adds methods to types you already use:
contextandwith_contexton results, for adding error contextor_aborton optionscanandauthorizefor authorizationfeaturesfor feature flagsRule::uniqueandRule::existsfor database validation rules- String helpers such as
snake, and date helpers such asis_past - Spans such as
5.minutes() with_cookieandwithout_cookieon responsesloadon a list of models andwithon a relation, for eager loading
These methods come from traits, so they only work in files that import the prelude. If the compiler says a method such as or_abort doesn't exist, check the file's imports.
Importing everything else
Import everything else from its own module, such as laravel::foundation::ServiceProvider or laravel::view::View. The first example in each section of these docs shows its imports, and the examples after it assume them.
The prelude leaves out Laravel's Result and Error types so they don't replace Rust's own. Write them as laravel::Result and laravel::Error, as the handler above does.
The testing prelude
In your tests, import the testing prelude instead. It includes everything in the prelude, plus what your tests need to exercise your application:
get,post, and the other request methods, to send requests to your applicationcommand, to run your console commandsassert_database_hasand the other database assertions- The
json!macro, for JSON request bodies and assertions
With that one import, a test can send a request and check what your application stored:
1use laravel::testing::prelude::*;23#[laravel::test]4async fn messages_can_be_posted(app: Application) {5 app.post("/messages")6 .json(json!({ "body": "Hello!" }))7 .await8 .assert_redirect();910 app.assert_database_has("messages", json!({ "body": "Hello!" })).await;11}1use laravel::testing::prelude::*;23#[laravel::test]4async fn messages_can_be_posted(app: Application) {5 app.post("/messages")6 .json(json!({ "body": "Hello!" }))7 .await8 .assert_redirect();910 app.assert_database_has("messages", json!({ "body": "Hello!" })).await;11}
When you're ready to write tests of your own, the testing guide walks you through everything the test client can do, from acting as a logged-in user and checking your database to faking HTTP requests and traveling through time.