Docs / Building
Fields
what a resource shows, in which view and to whom, and how each type is written.
Introduction #
A field is one attribute of a resource. Repository::fields lists them, in the order a response shows them. A response shows nothing of the model that no field lists, so a new column stays private until you add its field.
fn fields() -> Vec<Field<Post>> {
vec![
// No response shows the key as an attribute: it is the resource's `id`.
Field::query_only(Post::id).searchable().matchable().sortable(),
Field::new(Post::user_id).matchable_as(MatchKind::Array),
Field::new(Post::title).searchable().matchable().sortable(),
Field::new(Post::body).searchable().matchable().hide_from_index(),
Field::computed("excerpt", |post: &Post| post.body.as_deref().unwrap_or_default().limit(20)),
Field::new(Post::votes).matchable().sortable(),
Field::new(Post::featured)
.as_integer()
.matchable()
.can_see(is_admin)
.can_store(is_admin)
.can_update(is_admin),
Field::new(Post::rating).decimals(2).matchable_as(MatchKind::Between),
Field::new(Post::published_at).matchable().sortable(),
Field::new(Post::created_at).sortable(),
]
}
The same list says which columns clients may search, filter and sort by: see Filtering. What clients may write is decided by the input structs, and narrowed by the field's write gates: see Writing.
Showing a column #
Field::new(Post::title) shows the model's title column as the attribute title. The column is typed, so a column that doesn't exist doesn't compile. Restify reads the value from the model's field, not from its JSON, so the model's serde attributes don't change what a field shows.
| Constructor | Shows |
|---|---|
Field::new(Post::title) | A column |
Field::computed("excerpt", |post: &Post| ..) | A value computed from the model |
Field::computed_with("is_mine", is_mine) | A value computed from the model and the request |
Field::query_only(Post::id) | Nothing. Clients may filter or sort by the column |
Field::file(Document::attachment), Field::image(Document::cover) | The path of an uploaded file, or its URL. Clients upload the file in a multipart/form-data request. See files.md |
Which view shows a field #
There are four views: a list and one record, each over HTTP and as an MCP tool's answer. The answer of an endpoint that creates or updates shows what a list shows. The answer of an MCP tool that creates or updates shows the fields its input may write, as one record shows them.
| Method | Leaves the field out of |
|---|---|
.hide_from_index() | Lists, and the answers to a create or an update |
.hide_from_show() | One record |
.hide_from_mcp() | Every answer of an MCP tool |
PostRepository hides the post's body from lists, where the excerpt is enough.
Showing a field to some users #
can_see takes a gate: a bool, or an async function from the request to laravel::Result<bool>. A request that doesn't pass it gets the resource without the field, not an error.
/// Whether the request's user administers the blog. Fields use it as their `can_see`.
pub async fn is_admin(request: &Request) -> laravel::Result<bool> {
// The login and register routes have no guard, and there `request.user()` fails. A guest
// isn't an admin.
if !request.authenticated().await? {
return Ok(false);
}
let user: Option<User> = request.user().await?;
Ok(user.is_some_and(|user| user.is_admin))
}
Field::new(User::email).searchable().matchable().can_see(is_admin)
A gate runs once for a request, not once for each row of a list, so it can't depend on the model. For a value that depends on both, use computed_with.
can_see also closes the field's column to search, filters and sorting for the requests it refuses. If a user can't see email, then ?search=, ?email= and ?sort=email ignore that column for them, the same way they ignore a name the repository doesn't declare. Otherwise a user could guess a hidden value one letter at a time. This applies to the global search too. Restify for PHP still searches, filters and sorts by a hidden field. That is a deliberate difference.
?search= doesn't escape % and _, as in Restify for PHP. They act as wildcards: ?search=% matches every row. They only reach columns the request may see.
can_store, can_update, can_patch and readonly_when take a gate too. They decide who may write the field: a refused key is dropped from the input, and the rest of the write goes through. See Writing.
Computed fields #
Field::computed takes a name and a function from the model:
Field::computed("excerpt", |post: &Post| post.body.as_deref().unwrap_or_default().limit(20))
Field::computed_with takes an async function from the model and the request. It runs once for each record a response shows, so keep it cheap: the framework caches the request's user, and a query here runs for every row.
async fn is_mine(post: &Post, request: &Request) -> laravel::Result<bool> {
let user: Option<User> = request.user().await?;
Ok(user.is_some_and(|user| user.id == post.user_id))
}
Field::computed_with("is_mine", is_mine)
Clients can't write, search, filter by or sort by a computed field.
Changing what a field shows #
| Method | Does |
|---|---|
.label("headline") | Names the attribute. The column's name stays the name of the filter and the sort |
.default("Nothing written yet.") | Shows this value when the field's is null |
.resolve(|value, post| ..) | Changes the JSON before it is shown. It runs before default |
.as_integer() | Shows a boolean as 0 or 1 |
.decimals(2) | Shows a number as a string with exactly two decimals, such as "4.50" |
.description("..") | Describes the attribute in the OpenAPI document |
Field::new(Post::title).label("headline").sortable(),
Field::new(Post::body).default("Nothing written yet."),
Different fields for one view #
When one view needs a field defined differently, not just hidden, override fields_for. DraftRepository gives its lists their own fields:
/// A list shows the headline in capitals, and nothing else of a draft but its votes.
fn fields_for(view: View) -> Vec<Field<Post>> {
if view.is_index() {
return vec![
Field::new(Post::title)
.label("headline")
.resolve(|title, _post| Value::from(title.as_str().unwrap_or_default().to_uppercase())),
Field::new(Post::votes),
];
}
Self::fields()
}
Searching, filtering and sorting always read fields, whatever fields_for returns.
How each type is written #
A field writes its column's type the way Restify for PHP writes it, so a client of either reads the same JSON.
| Column type | JSON | Example |
|---|---|---|
String, Uuid, Ulid | A string | "Hello" |
| Integers | A number | 10 |
f32, f64 | A number | 4.5 |
bool | true or false. With .as_integer(), 1 or 0 | false |
Timestamp | UTC with six decimals of the second | "2026-01-03T12:30:00.000000Z" |
laravel::time::civil::Date, for a date column | The day | "2026-11-20" |
Decimal | A string, so no digit is lost. With .decimals(2), exactly two decimals | "4.50" |
Option<T> | null, or as T | null |
serde_json::Value, Vec<T> | As serde writes it | ["a", "b"] |
A date column holds a day and no time, so read it as laravel::time::civil::Date, as the example app's Milestone does. This works on SQLite, PostgreSQL, MySQL and MariaDB. The OpenAPI document and the MCP tools describe it as a string with "format": "date". Restify for PHP shows a column with Eloquent's date cast as midnight UTC, such as "2026-11-20T00:00:00.000000Z". To show that, add .resolve(|day, _| match day { Value::String(day) => Value::String(format!("{day}T00:00:00.000000Z")), other => other }) to the field.
.as_integer() is for a boolean column that the PHP application's model doesn't cast to boolean: on SQLite and MySQL, Restify for PHP then shows the 0 or 1 the database returns. The answer to a create or an update shows the value as sent, such as true, because Restify for PHP answers with the model it just filled.
To show a column of another type, such as an enum, implement restify::Property for it. Its property is the type's JSON Schema. Its wire writes the value and defaults to what serde writes.
Mistakes the application refuses to start with #
Restify checks every repository's fields when the application starts, and names the repository, the field and the fix:
- Two fields of one view have the same name. Remove one, or rename one with
label. - A computed field is marked
searchable,matchableorsortable. It has no column to query. - A field is marked
matchableormatchable_asbut its column's type can't read a filter's value, such asserde_json::Value. SetProperty::MATCHandProperty::parsefor the type, or write a scope. Every integer type,String,Uuid,Ulid,f64,Decimal,bool,Timestampandcivil::Datecan be read.