laravel-rust-admin
An admin panel for Laravel for Rust applications. Describe your models as resources, add one service provider, and your application serves a back office at /admin.
Each resource gets a table with search, filters, tabs, sorting, pagination, and CSV export, and forms that create and edit records through your own #[laravel::input] structs, so your validation rules decide what the panel can save. You can add actions on one record or on many, a dashboard of stats, charts, and tables, and policies for who may do what. A command palette (⌘K) searches every resource.
The frontend is built into the crate. Your application needs no Node toolchain, and the panel loads nothing from other hosts.
The panel has a light and a dark appearance, and these screenshots show the one that matches your GitHub theme. For a tour with more of them, and the code behind each, open docs/index.html from a checkout.
[!NOTE] This is early work, and the API will change. The panel is built against
laravel-rust0.1 and has only been run on SQLite so far.
Try the demo
The repository includes a small shop's back office with orders, customers, and products:
git clone https://github.com/n7olkachev/laravel-rust-admin
cd laravel-rust-admin
cargo run -p laravel-rust-admin-demo
Then open http://127.0.0.1:8000/admin. Set SERVER_PORT to serve on another port. The demo keeps its data in an in-memory database that it creates and fills on every start.
Installation
cargo add laravel-rust-admin
The panel needs Rust 1.99 or newer, as Laravel for Rust does, and uses whichever database driver your application enables on laravel-rust.
Quick start
The quickest resource is a derive on a model. Its table and its form come from the model's fields:
use model;
use Timestamp;
use AdminResource;
The panel only creates and updates records through the input structs you name. Their rules run on every save, their messages appear under the form's fields, and a field the struct doesn't declare can't be written, whatever the browser sends. A resource without create or update is read-only.
Then add the panel to your providers in src/app.rs, and say who may enter:
use Application;
use Authenticate;
use Request;
use AdminPanel;
async
configure
.with_providers
The derive's documentation lists every #[admin(..)] option, and what the derive works out from a field's type and name.
Who may use the panel
A panel is only as safe as the answer to "who may enter", so the panel insists on one:
middleware(..)attaches middleware to every panel route, such asAuthenticate.authorize(..)takes anasync fn(&Request) -> laravel::Result<bool>that runs before every panel request, after the middleware. Returningfalseresponds with a403.
A panel with neither would be open to anyone who finds its URL. Such a panel only answers in the local and testing environments. Everywhere else it responds with a 403, and logs a warning at startup, until you add one of the two.
user_resolver(..) tells the panel who is using it, for the user menu and the dashboard's greeting. logout_url(..) adds "Log out" to that menu. The panel sends it a POST with the CSRF token.
Every write the panel makes carries the session's CSRF token, so the panel works with Laravel's PreventRequestForgery middleware as it is.
Resources by hand
Implement AdminResource yourself, usually on a unit struct, when a resource needs more than the derive offers. This is the demo's orders resource, shortened:
use Builder;
use Request;
use update;
use *;
;
The table
A column's name is a field of the serialized model. Column has a constructor for each way to show a value: text, badge, boolean, number, currency, datetime, date, relative_time, avatar, tags, and json.
- Relationships. Load a relationship in
query, and name a column with a dotted path, such ascustomer.name. The database can't sort or search by such a column, and the panel refuses to start if you ask it to. - Search. The table's search box, and the command palette, look in the
searchable()columns and in the columns you pass tosearch_by([..]), which don't have to be shown. - Filters.
Filter::select,Filter::boolean,Filter::date_range, andFilter::textappear as chips above the table. Each can replace how it narrows the query with.query(..). - Tabs. A
TableTabnarrows the table to some of its records, and shows how many there are. - Export. "Export" downloads every record that matches the current search, filters, and tab as CSV. The export is streamed, so it may be of any size, and cells that a spreadsheet would run as formulas are neutralized.
The table's state lives in the URL, so a filtered view can be bookmarked or shared.
The form
Field has a constructor for each kind of input: text, email, url, password, textarea, number, decimal, select, radio, toggle, checkbox, date, datetime, civil_datetime, tags, json, and readonly. Group fields into a FormSection to give them a heading and a layout of one to three columns.
The form is only the form. What gets saved is decided by the resource's CreateInput and UpdateInput.
Actions
A RowAction runs on one record and a BulkAction on every selected record. An action can ask for confirmation, and can collect fields before it runs, which its handler reads from the ActionContext. The handler returns an ActionOutcome with the message to show. An error's message is shown instead.
available_when(..) says which records a row action applies to, such as "ship" only for an order that is paid. The panel leaves the action out of every other record's menu, and refuses to run it on them.
Authorization
Every authorize_* method of AdminResource allows everything until you override it: authorize_view_any, authorize_view, authorize_create, authorize_update, authorize_delete, and authorize_action. Each receives the request, so it can check a policy with request.can(..). The panel enforces them on the server, and uses their answers to hide what the user may not do.
To limit which records a user sees at all, such as their own team's, narrow query. A record the query leaves out doesn't exist as far as the panel is concerned: it's in no table, search, or export, and its URL responds with a 404.
The dashboard
A widget is an async function that returns a StatWidget, a ChartWidget, or a TableWidget. It runs each time the dashboard loads:
async
new.widget_fn
widget_for_request takes a function of the request instead, for a widget that shows the logged-in user's own numbers. A panel without widgets shows a count of each resource's records.
Configuration
| Builder method | Environment variable | Sets |
|---|---|---|
path("/admin") |
ADMIN_PATH |
Where the panel is mounted |
brand("Acme") |
ADMIN_BRAND |
The name in the sidebar and the page title |
subtitle("Back office") |
A line under the brand | |
accent(AccentColor::Indigo) |
The color of primary buttons and checked controls | |
logout_url("/logout") |
The "Log out" item of the user menu |
The panel follows the operating system's light or dark appearance until the user picks one from the user menu.
How it's put together
The crate serves a single-page app from files embedded in the binary, and a JSON API under {path}/api that the app talks to. The app's types mirror the Rust types in src/protocol.rs by hand for now. They are meant to be generated by laravel-rust-typescript-bridge.
The panel's page works with a Content Security Policy: its inline script carries the nonce from laravel::view::csp_nonce(). Its font, Inter, ships with the crate, and dist/THIRD-PARTY-LICENSES.txt holds the licenses of everything in the bundle.
The panel only knows each model as M: Model, so it can't call the query, create, update, and delete methods that #[model] gives each struct. It calls what those methods are made of instead, which Laravel exports but doesn't document. All four uses are in src/model.rs, so that a Laravel release that changes them changes one file.
Working on the frontend
The frontend lives in frontend/ and is built with Bun, React, Tailwind CSS, and Vite into crates/laravel-rust-admin/dist, which is committed so that the crate builds without Node.
cd frontend
bun install
bun run dev # the Vite dev server, with hot reloading
bun run test # unit tests
bun run build # type-check, then write the bundle the crate embeds
To see your changes as you make them, run the demo against the dev server:
ADMIN_VITE_DEV_URL=http://localhost:5173 cargo run -p laravel-rust-admin-demo
The Rust side has its own tests, which run a real application against an in-memory database:
cargo test -p laravel-rust-admin --features sqlite
Not there yet
- PostgreSQL and MySQL are untested. The queries go through Laravel's query builder, but nothing has run on those drivers.
- A form can't choose a related record. A foreign key is a plain number field for now.
- No file uploads.
- One panel per application.
- Selecting rows selects the rows on the current page, not every row that matches.
- A
Filter::selectcompares its column as text. - The panel is in English only.
Credits
The panel takes after Filament in what it does and after Flux in how it looks. It shares no code with either, and isn't affiliated with them or with Laravel.
License
MIT