#[derive(Model)]
{
// Attributes available to this derive:
#[table]
#[primary_key]
#[route_key]
#[timestamps]
#[soft_deletes]
#[observe]
#[has_many]
#[has_one]
#[belongs_to]
#[belongs_to_many]
#[has_many_through]
}
Expand description
#[derive(FormRequest)], view!, error_view!, and #[derive(Model)] -
re-exported here so generated apps depend only on larust-support, not
on larust-macros directly. All four macros’ generated code assumes
this re-export path (::larust_support::...).
#[derive(Model)]: generates TABLE/field-name constants, query(),
all(), find(id), create(data), and delete(id) over
larust-orm’s QueryBuilder, plus route model binding - an
axum::extract::FromRequestParts impl so a handler can declare
post: Post on a route like /posts/{post} and have it auto-resolved
(404 if not found). Requires #[table("...")] on the struct and
exactly one #[primary_key] field (currently must be i64).
#[route_key("slug")] looks records up by that field instead of the
primary key. #[timestamps] (Laravel’s $table->timestamps()) opts
into automatic created_at/updated_at population - both must exist
as i64 (Unix seconds) fields; create() stamps both, update()
re-stamps updated_at only, and neither appears in the generated
New* struct, matching Eloquent’s own “never set these yourself”
behavior.
#[soft_deletes] (Laravel’s SoftDeletes trait) opts into a nullable
deleted_at: Option<i64> field: delete() sets it instead of
removing the row, every query()/find()/all()/route-model-binding
lookup transparently excludes trashed rows (QueryBuilder::{ with_trashed, only_trashed} opt back in), and force_delete/
restore/find_with_trashed/.trashed() round out the rest.
#[observe(creating = ..., created = ..., updating = ..., updated = ..., deleting = ..., deleted = ...)] (Laravel’s model-event observers) runs a
plain function around create/update/delete - creating/updating
transform (or veto, by returning Err) the data before the write;
created/updated/deleted run after, with the result; deleting
runs before, with the row fetched fresh for the purpose. Fixed hook
signatures, checked by rustc at the generated call site - a
mismatch is a compile error pointing at the attribute itself, not a
trait-impl error several layers removed from it.
#[has_many(...)]/#[has_one(...)]/#[belongs_to(...)] (repeatable)
generate Laravel-style relationship accessor methods, both a lazy
per-instance form and a batch/eager load_* form; #[belongs_to_many( ...)] (repeatable) generates a many-to-many relationship through a
pivot table (tags()/attach_*/detach_*/sync_*, no eager-loading
form yet); #[has_many_through(...)] (repeatable) generates a 3-table
relationship (Laravel’s hasManyThrough) as a composable
{method}_query() plus a {method}() convenience wrapper, built on
QueryBuilder::where_in_query rather than a real JOIN (also no
eager-loading form yet) - see docs/MACROS.md for the full grammar
and generated shapes.
#[derive(Model, sqlx::FromRow)]
#[table("posts")]
#[belongs_to(User, foreign_key = "user_id")]
#[belongs_to_many(Tag, through = "post_tag", foreign_key = "post_id", related_pivot_key = "tag_id")]
pub struct Post {
#[primary_key]
pub id: i64,
pub user_id: i64,
pub title: String,
}