laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Models, which are structs whose values are rows of a table, with typed columns and queries.
//!
//! Most of an application's work is reading and writing rows, such as users and their posts.
//! [`#[model]`](model) makes a struct the model of a table. The struct gets a typed [`Column`]
//! constant for each field and queries that return the struct. It also gets creates and updates
//! whose values the compiler checks against the columns' types, and relations that load related
//! models.
//!
//! ```no_run
//! # async fn example() -> laravel::Result<()> {
//! use laravel::model::model;
//! use laravel::time::Timestamp;
//!
//! #[model]
//! pub struct User {
//!     pub id: i64,
//!     pub name: String,
//!     pub email: String,
//!     pub votes: i64,
//!     pub created_at: Timestamp,
//!     pub updated_at: Timestamp,
//! }
//!
//! let popular: Vec<User> = User::query()
//!     .filter(User::votes.gt(100) & User::email.ends_with("@laravel.com"))
//!     .order_by(User::name)
//!     .get()
//!     .await?;
//! # Ok(()) }
//! ```
//!
//! To learn how to declare, query, and write models, check out the
//! [models guide](https://github.com/laravel/rust-framework/blob/0.x/docs/models.md). To learn
//! about relations, check out the
//! [relationships guide](https://github.com/laravel/rust-framework/blob/0.x/docs/model-relationships.md).

mod route;
mod users;

pub(crate) use laravel_database::model::*;
#[doc(hidden)]
pub use laravel_database::model::{
    __BelongsTo, __Connection, __Counts, __Embed, __Embedded, __Has, __HasMany, __MorphVariant, __PicksOrOwns,
    __Reaches, __ReachesRows, __Related, __RelationMethod, __Text, __Variant, __concat_fields, __create_many, __delete,
    __destroy, __distinct_morphs, __find, __first_or_create, __for_morph, __fresh, __hop, __insert, __is, __key,
    __load, __method_id, __morph_pair, __prefixed_bytes, __prefixed_len, __prefixed_names, __query, __refresh,
    __require_columns, __serde, __stamp, __update_or_create, __update_values, __upsert, __value, AnyRelation,
    Assignments, Children, ColumnsOf, CreateBuilder, Decrement, Definition, Embed, FillRef, FillTarget, Hop, Increment,
    Inverse, IsMorphQuery, JoinColumn, JoinItem, JoinMorph, JoinMorphField, JoinMorphPath, JoinRows, JoinTable,
    MassUpdateBuilder, MorphField, MorphPair, MorphParent, MorphShared, MorphSlot, MorphToRelation, Null, OfMany,
    Parent, ParentQuery, QueryTable, RelationKeys, Scope, SerdeRename, Shared, Slot, UpdateBuilder, Updates, Variant,
    joined,
};
#[doc(inline)]
pub use laravel_database::model::{
    BelongsTo, BelongsToMany, Change, Changes, ChildrenQuery, Column, Columns, ComparableList, ComparableTo,
    CreateMany, Created, Deleted, Draft, Eager, Factory, FakeInteger, Faker, Field, Fill, FillValue, FindOrCreate,
    ForMorph, ForParent, ForeignKey, FoundOrCreated, HasMany, HasManyThrough, HasOne, HasOneThrough, Hooks,
    IntoAggregate, IntoChildren, IntoCount, IntoCreate, IntoEager, IntoHas, IntoJoinValue, IntoKeys, IntoModels,
    IntoMorph, IntoMorphParent, IntoParent, IntoRow, JoinMorphColumns, JoinRow, JoinValues, Joined, JoinedQuery,
    KeySource, Many, ManyToMany, Meta, Model, ModelKey, ModelList, ModelNotFound, Morph, MorphEager, MorphKey,
    MorphMany, MorphOf, MorphOne, MorphQuery, MorphRelation, MorphTo, MorphToMany, MorphType, MorphedByMany, Numeric,
    Ordered, Relation, RelationCount, RelationQuery, Replica, Stamp, SyncChanges, Unique, UpdateValue, Updated,
    without_events,
};

/// Derives [`Fill`], which fills a model's columns from a struct's fields of the same names.
///
/// A create's or an update's `fill` takes the struct, and so does `..input` in [`create!`] and
/// [`update!`]. `#[fills(User)]` names the model. It may also name a [`JoinRow`], such as
/// `#[fills(Membership)]`. Then `..input` fills the join row's values in [`joined!`], and `fill`
/// fills them on the join row's values builder.
///
/// Each field fills the model's column of the same name, or the column that
/// `#[fill(column = "..")]` names. A field marked `#[fill(skip)]` fills no column. A field with no
/// matching column, or with a type that can't fill the column, is a compile error. A `String` or
/// [`Password`](crate::hash::Password) field fills a [`PasswordHash`](crate::hash::PasswordHash)
/// column with its hash. A [`Sometimes`](crate::validation::Sometimes) field fills its column only
/// when the request sent the field.
///
/// A reference to the struct fills the same columns with clones of the fields, so the struct stays
/// usable, as in `..&input`. The reference implements `Fill` when the struct is `Sync` and every
/// filled field is `Clone`.
///
/// ```no_run
/// # use laravel::model::model;
/// # use laravel::hash::PasswordHash;
/// # #[model] pub struct User { pub id: i64, pub name: String, pub email: String, #[serde(skip)] pub password: PasswordHash }
/// #[laravel::input(fills = User)]
/// pub struct Register {
///     #[validate(required, max_length = 255)]
///     pub name: String,
///     #[validate(required, email)]
///     #[fill(column = "email")]
///     pub address: String,
///     #[validate(required, min_length = 8)]
///     pub password: String,
///     #[fill(skip)]
///     pub newsletter: bool,
/// }
/// ```
///
/// ```compile_fail
/// # use laravel::model::{Fill, model};
/// # #[model] pub struct User { pub id: i64, pub name: String }
/// #[derive(Fill)]
/// #[fills(User)]
/// pub struct Rename {
///     pub nickname: String,
/// }
/// ```
pub use laravel_rust_model_macros::Fill;

/// Creates models with struct syntax.
///
/// The macro returns the model's create builder, so you may chain `.quietly()`. Awaiting the
/// builder inserts the row and returns the model.
///
/// ```no_run
/// # use laravel::model::{BelongsTo, Fill, HasMany, model};
/// # #[model] pub struct User { pub id: i64, pub name: String, #[has_many] pub posts: HasMany<Post> }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, pub title: String, pub body: Option<String>, #[belongs_to] pub user: BelongsTo<User> }
/// # #[derive(Fill)] #[fills(Post)] pub struct NewPost { pub title: String }
/// # async fn example(user: User, input: NewPost, other: NewPost, body: String) -> laravel::Result<()> {
/// use laravel::model::create;
///
/// let post = create!(Post { title: "Hello", body, user: &user }).await?;
/// let post = create!(Post { user: &user, ..input }).await?;
/// let post = create!(user.posts() { ..other }).quietly().await?;
/// let posts = create!([user.posts() { title: "One" }, user.posts() { title: "Two" }]).await?;
/// let user = create!(User { name: "Taylor", posts: [Post { title: "One" }] }).await?;
/// let post = create!(Post { title: "Hi", user: User { name: "Abigail" } }).await?;
/// # Ok(()) }
/// ```
///
/// | Syntax | Builder call |
/// | --- | --- |
/// | `Post { .. }` | `Post::create()` |
/// | `user.posts() { .. }` | `user.posts().create()`, which sets the foreign key, or attaches through a join table |
/// | `title: "Hello"` | `.title("Hello")` |
/// | `body` | `.body(body)` |
/// | `user: &user` | `.user(&user)`, which sets `user_id` |
/// | `..input` | `.fill(input)`, before the other fields, so they replace its values |
/// | `..&input` | `.fill(&input)`, which clones the filled fields and leaves `input` usable |
/// | `title, ..`, with a bare `..` last | `.title(title)`, without the compile-time check for required columns |
/// | `posts: [Post { .. }, ..]` | `.posts([..])`, which creates the posts after the user |
/// | `user: User { .. }` | `.user(..)`, which creates the user before the post |
/// | `[Post { .. }, ..]` | [`CreateMany`], which returns a `Vec` |
///
/// A create with related models runs in one transaction, and so does a list of creates. Inside an
/// existing transaction, the create runs in a savepoint. To give a field the value of a struct
/// literal, put the value in parentheses.
///
/// Leaving out a required column is a compile error, such as
/// ``can't create a `Post` without `user` (or `user_id`) and `title` ``. The check runs when you
/// await the create instead in these cases: the model has `hooks`, the create goes through a
/// relationship, or the fields end with `..input` or a bare `..`. The create then fails before it
/// inserts the row.
///
/// ```compile_fail,E0080
/// # use laravel::model::{BelongsTo, create, model};
/// # #[model] pub struct User { pub id: i64 }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, pub title: String, pub body: Option<String>, #[belongs_to] pub user: BelongsTo<User> }
/// # async fn example() -> laravel::Result<()> {
/// let post = create!(Post { body: "No title" }).await?;
/// # Ok(()) }
/// ```
pub use laravel_rust_model_macros::create;

/// Makes a unit struct the factory of a model, which builds the model's default row for tests and
/// seeders.
///
/// The struct's `definition` returns the row, usually as a [`create!`] literal, and its other
/// methods are states. `#[factory(User)]` adds `User::factory()`, which returns the factory:
///
/// ```no_run
/// use laravel::model::{Faker, IntoCreate, create, factory, model};
/// use laravel::time::Timestamp;
///
/// #[model]
/// pub struct User {
///     pub id: i64,
///     pub name: String,
///     pub email: String,
///     pub email_verified_at: Option<Timestamp>,
/// }
///
/// #[factory(User)]
/// pub struct UserFactory;
///
/// impl UserFactory {
///     fn definition(fake: &mut Faker) -> impl IntoCreate<User> {
///         create!(User { name: fake.name(), email: fake.unique().email(), email_verified_at: laravel::now() })
///     }
///
///     pub fn unverified(self) -> Self {
///         self.state(|user, _| user.email_verified_at(None::<Timestamp>))
///     }
/// }
///
/// # async fn example() -> laravel::Result<()> {
/// let user = User::factory().create().await?;
/// let users = User::factory().unverified().count(3).create().await?;
/// let taylor = create!(User::factory() { name: "Taylor" }).await?;
/// # Ok(()) }
/// ```
///
/// The macro gives the struct [`Factory`]'s methods: `state`, `has`, `for_`, `recycle`,
/// `after_creating`, `count`, and `create`. It also implements these traits for the factory:
///
/// - [`IntoParent`], so a definition may give a `#[belongs_to]` field another factory, as in
///   `user: User::factory()`.
/// - [`ForParent`], so `for_` accepts the factory.
/// - [`IntoChildren`], so `has` accepts the factory.
///
/// The compiler checks a factory's relations. `has` only accepts factories of the relation's
/// model:
///
/// ```compile_fail
/// # use laravel::model::{Faker, HasMany, IntoCreate, create, factory, model};
/// # #[model] pub struct User { pub id: i64, pub name: String, #[has_many] pub posts: HasMany<Post> }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64 }
/// # #[model] pub struct Tag { pub id: i64, pub name: String }
/// # #[factory(User)] pub struct UserFactory;
/// # impl UserFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<User> { create!(User { name: fake.name() }) } }
/// # #[factory(Tag)] pub struct TagFactory;
/// # impl TagFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<Tag> { create!(Tag { name: fake.word() }) } }
/// User::factory().has(User::posts, Tag::factory());
/// ```
///
/// `for_` only accepts a `#[belongs_to]` relation:
///
/// ```compile_fail
/// # use laravel::model::{Faker, BelongsTo, HasMany, IntoCreate, create, factory, model};
/// # #[model] pub struct Post { pub id: i64, pub title: String, #[has_many] pub comments: HasMany<Comment> }
/// # #[model] pub struct Comment { pub id: i64, pub post_id: i64, #[belongs_to] pub post: BelongsTo<Post> }
/// # #[factory(Post)] pub struct PostFactory;
/// # impl PostFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<Post> { create!(Post { title: fake.sentence() }) } }
/// # #[factory(Comment)] pub struct CommentFactory;
/// # impl CommentFactory { fn definition(_: &mut Faker) -> impl IntoCreate<Comment> { create!(Comment { post: Post::factory() }) } }
/// Post::factory().for_(Post::comments, Comment::factory());
/// ```
///
/// `for_` accepts a model or a factory, but not a create, which would make one model for every
/// `create`:
///
/// ```compile_fail
/// # use laravel::model::{Faker, BelongsTo, IntoCreate, create, factory, model};
/// # #[model] pub struct User { pub id: i64, pub name: String }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, #[belongs_to] pub user: BelongsTo<User> }
/// # #[factory(User)] pub struct UserFactory;
/// # impl UserFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<User> { create!(User { name: fake.name() }) } }
/// # #[factory(Post)] pub struct PostFactory;
/// # impl PostFactory { fn definition(_: &mut Faker) -> impl IntoCreate<Post> { create!(Post { user: User::factory() }) } }
/// Post::factory().for_(Post::user, create!(User { name: "Taylor" }));
/// ```
///
/// Through a `#[has_one]` relation, `has` accepts one factory, but not `count` of one:
///
/// ```compile_fail
/// # use laravel::model::{Faker, BelongsTo, HasOne, IntoCreate, create, factory, model};
/// # #[model] pub struct User { pub id: i64, pub name: String, #[has_one] pub profile: HasOne<Option<Profile>> }
/// # #[model] pub struct Profile { pub id: i64, pub user_id: i64, pub bio: String }
/// # #[factory(User)] pub struct UserFactory;
/// # impl UserFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<User> { create!(User { name: fake.name() }) } }
/// # #[factory(Profile)] pub struct ProfileFactory;
/// # impl ProfileFactory { fn definition(fake: &mut Faker) -> impl IntoCreate<Profile> { create!(Profile { bio: fake.sentence(), user_id: 1 }) } }
/// User::factory().has(User::profile, Profile::factory().count(2));
/// ```
///
/// These checks read the related model's path as written. `#[model]` treats two paths as the same
/// model when one ends with the other, so `User` and `crate::models::User` match. When a model
/// relates to two different models with the same name, such as `Account` and `billing::Account`,
/// write both paths in full, as `auth::Account` and `billing::Account`. Otherwise, `for_` and
/// `has` through the second relation fail to compile.
///
/// Like any [`create!`], a definition must set every column that has no default:
///
/// ```compile_fail
/// # use laravel::model::{Faker, IntoCreate, create, factory, model};
/// # #[model] pub struct User { pub id: i64, pub name: String, pub email: String }
/// #[factory(User)]
/// pub struct UserFactory;
///
/// impl UserFactory {
///     fn definition(fake: &mut Faker) -> impl IntoCreate<User> {
///         create!(User { name: fake.name() })
///     }
/// }
/// ```
///
/// A factory runs inside the application, which holds the values that [`Faker::unique`] has
/// returned and the hash of each password that the factory's definitions and states set. Every
/// user that sets `password: "password"` shares one hash.
pub use laravel_rust_model_macros::factory;

/// Makes a struct of a join table's columns a [`JoinRow`].
///
/// Like [`#[model]`](model), the attribute derives `Clone`, `Debug`, and serde's `Serialize`.
pub use laravel_rust_model_macros::join_row;

/// Makes a struct a model, whose values are rows of a table, with a typed column for each field.
///
/// ```no_run
/// use laravel::model::model;
/// use laravel::time::Timestamp;
/// use uuid::Uuid;
///
/// #[model(table = "people", connection = "reporting", route_key = "slug")]
/// pub struct Person {
///     #[key]
///     pub uuid: Uuid,
///     pub slug: String,
///     #[column("is_admin")]
///     pub admin: bool,
///     #[default]
///     pub votes: i64,
///     pub created_at: Timestamp,
///     pub updated_at: Timestamp,
/// }
/// ```
///
/// | Option or attribute | Meaning |
/// | --- | --- |
/// | `table = ".."` | The table. By default, the plural of the struct's name in snake case, so `BlogPost` is `blog_posts` |
/// | `connection = ".."` | The connection queries run on, instead of the default connection |
/// | `route_key = ".."` | The field that route model binding finds the model by, instead of the key. A route's `{post:slug}` names another for that route |
/// | `hooks` | You implement [`Hooks`] for the model. Without it, the macro implements the defaults |
/// | `#[key]` | The key field, instead of `id` |
/// | `#[column("..")]` | The field's column, instead of the field's name |
/// | `#[default]` | The database supplies the column's value when an insert leaves it out |
/// | `#[embed]` | A [`db::Embed`](crate::db::Embed) field, a column per field of its struct, named after the field, as `total_amount` for `total`. Takes `prefix = ".."` |
/// | `#[belongs_to]` | A [`BelongsTo`] field. Takes `foreign_key` and `owner_key` |
/// | `#[has_one]`, `#[has_many]` | A [`HasOne`] or [`HasMany`] field. Takes `foreign_key` and `local_key`. `#[has_one]` also takes one of `latest_of_many`, `oldest_of_many`, and `of_many` |
/// | `#[belongs_to_many]` | A [`BelongsToMany`] field. Takes `table`, `foreign_join_key`, and `related_join_key` |
/// | `#[has_one_through(A::first, B::second)]`, `#[has_many_through(..)]` | A [`HasOneThrough`] or [`HasManyThrough`] field, which reaches its rows through the model's relation `first` and the intermediate model's relation `second`. `#[has_one_through]` takes `latest_of_many`, `oldest_of_many`, and `of_many` |
///
/// A relation that holds one model may pick one of many related rows. `latest_of_many` and
/// `oldest_of_many` pick the row with the largest or smallest key, or with the largest or
/// smallest value of the column they name, as in `latest_of_many = Order::placed_at`.
/// `of_many(Order::price, max)` names the column and `max` or `min`. Ties go to the larger or
/// smaller key, and rows whose column is NULL are never picked. A condition on such a relation,
/// in `has_where` or an eager load's `query`, applies to the row it picked.
///
/// Every relation attribute except `#[morph_to]` accepts `filter`, a condition that every related
/// row meets, as in `#[has_many(filter = !Post::published_at.is_null())]`. The relation's query,
/// `with`, `with_count`, `has`, `has_where`, `doesnt_have_where`, nested route bindings, and
/// through relations that name the relation only read the rows that meet the filter, and
/// `latest_of_many` picks among those rows. A `#[belongs_to_many]` filter may name its join row's
/// columns.
///
/// A create through the relation sets the columns that the filter compares with `eq`. A
/// `#[belongs_to_many]` relation's `attach`, `sync`, and `toggle` set those columns on the join
/// rows they insert. Its `detach`, `sync`, `toggle`, and `update_join_row` only touch the join
/// rows of related models that meet the filter.
///
/// ```no_run
/// # async fn example() -> laravel::Result<()> {
/// use laravel::model::{HasMany, create, model};
///
/// #[model]
/// pub struct Post {
///     pub id: i64,
///     pub title: String,
///     #[has_many(filter = Comment::approved.eq(true))]
///     pub approved_comments: HasMany<Comment>,
/// }
///
/// #[model]
/// pub struct Comment {
///     pub id: i64,
///     pub post_id: i64,
///     pub body: String,
///     pub approved: bool,
/// }
///
/// let post = Post::query().with(Post::approved_comments).find_or_fail(1).await?;
/// assert!(post.approved_comments.iter().all(|comment| comment.approved));
///
/// let comment = create!(post.approved_comments() { body: "Thanks!" }).await?;
/// assert!(comment.approved);
/// # Ok(()) }
/// ```
///
/// A relation field isn't a column. Rows read from the database leave it unloaded, and `update`
/// doesn't write it. For each relation field, the macro adds a method of the same name. Calling
/// the method starts a query of the related rows, such as `user.posts()`, and the method as a
/// value names the relation, such as `User::posts` in `with`.
///
/// A loaded relation serializes under the field's name, such as `posts`. A `with_count` count
/// serializes under the name with `_count` added, such as `posts_count`. An unloaded relation is
/// left out. When the struct derives `Deserialize`, it reads both keys back, so the model has the
/// same relations loaded and counted.
///
/// The macro derives `Clone`, `Debug`, and serde's `Serialize`, so the struct doesn't derive them
/// itself. Every column serializes, so mark the columns that must stay on the server with
/// `#[serde(skip)]`. Derive `Deserialize` when you read the model back, such as from a cache.
///
/// The macro implements [`Model`], [`FromRow`](crate::db::FromRow), and
/// [`RouteModel`](crate::routing::RouteModel), so a handler can take the model as an argument. It
/// also implements [`Hooks`] unless the model has `hooks`. It adds a [`Column`] constant for each
/// field, such as `Person::slug`, and the methods `query`, `all`, `find`, `find_or_fail`,
/// `find_many`, `first_where`, `create`, `destroy`, `first_or_create`, `update_or_create`,
/// `upsert`, `update`, `increment`, `decrement`, `delete`, `delete_quietly`, `fresh`, `refresh`,
/// `is`, `is_not`, `without_relations`, `replicate`, and `load`.
///
/// A field can't use one of those names, or `fill` or `quietly`, which the builders have. Give
/// the field another name, and keep the column's name with `#[column("..")]`.
///
/// `create` returns a create builder, such as `PersonCreate`, and `update` returns an update
/// builder, such as `PersonUpdate`. Each builder has a setter for each column, named after the
/// field, and a setter for each `#[belongs_to]` relation that takes the related model. A create
/// builder also has a setter for each `#[has_one]`, `#[has_many]`, and `#[belongs_to_many]`
/// relation. That setter takes create builders of the related model, which the create runs after
/// it inserts the row. [`create!`] and [`update!`] build these builders from struct syntax.
///
/// `created_at` and `updated_at` fields of type `Timestamp` or `Option<Timestamp>` hold the times
/// that creates and updates set. The times come from [`laravel::now`](crate::now), so time travel
/// in a test changes them. An update truncates them to the connection's
/// [`time_precision`](crate::config::database::ConnectionConfig::time_precision), as the database
/// stores them, so the model equals its fresh copy. A create or an update that sets a timestamp
/// keeps that value.
///
/// A model query's `update`, `increment`, and `decrement` set `updated_at` unless the values set
/// it. Its `upsert` sets both timestamps on new rows and `updated_at` on updated rows. Its
/// `insert` sets neither.
///
/// On MariaDB, a create needs version 10.5 or later.
///
/// Each write of one model dispatches [`Created`], [`Updated`], or [`Deleted`] after the write. A
/// listener's error reaches the code that wrote the model, after the write. `quietly` on a create
/// with related models mutes the events of every row it creates.
///
/// A [`Queueable`](derive@crate::queue::Queueable) job, a queued listener's event, or a callback
/// may hold a model, which the queue stores as its key. A queued listener may take [`Created`]
/// and [`Updated`], but not [`Deleted`], because the row no longer exists:
///
/// ```compile_fail
/// # use laravel::model::{Deleted, model};
/// # #[model] pub struct User { pub id: i64 }
/// use laravel::event::{Event, listener};
///
/// #[listener(queued)]
/// async fn archive(event: Event<Deleted<User>>) {}
/// ```
pub use laravel_rust_model_macros::model;

/// Declares the models that a `#[morph_to]` relation may hold, as an enum with one variant for
/// each model, such as the posts and videos that a comment belongs to.
///
/// ```no_run
/// use laravel::model::{MorphMany, MorphTo, MorphType, model, morph};
///
/// #[morph]
/// pub enum Commentable {
///     Post(Post),
///     Video(Video),
/// }
///
/// #[model]
/// pub struct Comment {
///     pub id: i64,
///     pub body: String,
///     pub commentable_type: MorphType<Commentable>,
///     pub commentable_id: i64,
///     #[morph_to]
///     pub commentable: MorphTo<Commentable>,
/// }
///
/// #[model]
/// pub struct Post {
///     pub id: i64,
///     #[morph_many(Comment::commentable)]
///     pub comments: MorphMany<Comment>,
/// }
///
/// #[model]
/// pub struct Video {
///     pub id: i64,
///     #[morph_many(Comment::commentable)]
///     pub comments: MorphMany<Comment>,
/// }
/// ```
///
/// The enum is the relation's morph map. The relation's type column stores each model's morph
/// name, such as `post` for `Post`. The name defaults to the struct's name in snake case, and
/// `#[model(morph = "..")]` sets a different one. Every variant's model must have the same key
/// type, and no two variants may share a morph name.
///
/// Like `#[model]`, the attribute derives `Clone`, `Debug`, and serde's `Serialize`. A value
/// serializes as the model it holds, with no tag, because the row's type column says which model
/// it is. When a model derives `Deserialize`, its `#[morph_to]` enum must derive it too. The enum
/// reads each value as the first variant whose model matches it.
///
/// ```compile_fail
/// # use laravel::model::{model, morph};
/// # #[model] pub struct Post { pub id: i64 }
/// # #[model] pub struct Video { pub id: laravel::str::Uuid }
/// #[morph]
/// pub enum Commentable {
///     Post(Post),
///     Video(Video),
/// }
/// ```
pub use laravel_rust_model_macros::morph;

/// Writes a row of a model for its `insert` and `upsert`.
///
/// The macro accepts the fields of [`create!`]: the columns, `user: &user` for an existing
/// `#[belongs_to]` parent, and `..input` to fill from a [`Fill`] struct. A list,
/// `row!([Post { .. }, ..])`, returns an array of rows.
///
/// ```no_run
/// # use laravel::model::{BelongsTo, model};
/// # #[model] pub struct User { pub id: i64 }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, pub slug: String, pub title: String, #[belongs_to] pub user: BelongsTo<User> }
/// # async fn example(user: User) -> laravel::Result<()> {
/// use laravel::model::row;
///
/// Post::insert(row!([
///     Post { slug: "hello", title: "Hello", user: &user },
///     Post { slug: "again", title: "Again", user: &user },
/// ]))
/// .await?;
///
/// Post::upsert(
///     [row!(Post { slug: "hello", title: "Hello, world", user: &user })],
///     [Post::slug],
///     [Post::title],
/// )
/// .await?;
/// # Ok(()) }
/// ```
///
/// `insert` and `upsert` run no hooks and create no related models, so they don't accept
/// `create!`, and a related model's struct literal in `row!` is a compile error. Leaving out a
/// required column is also a compile error, unless the fields end with `..input`. The method
/// checks a fill struct's row before the statement runs.
///
/// ```compile_fail
/// # use laravel::model::{create, model};
/// # #[model] pub struct Post { pub id: i64, pub title: String }
/// # async fn example() -> laravel::Result<()> {
/// Post::insert([create!(Post { title: "Hello" })]).await?;
/// # Ok(()) }
/// ```
///
/// ```compile_fail,E0080
/// # use laravel::model::{model, row};
/// # #[model] pub struct Post { pub id: i64, pub title: String, pub body: Option<String> }
/// # async fn example() -> laravel::Result<()> {
/// Post::insert([row!(Post { body: "No title" })]).await?;
/// # Ok(()) }
/// ```
///
/// ```compile_fail
/// # use laravel::model::{BelongsTo, model, row};
/// # #[model] pub struct User { pub id: i64, pub name: String }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, pub title: String, #[belongs_to] pub user: BelongsTo<User> }
/// # async fn example() -> laravel::Result<()> {
/// Post::insert([row!(Post { title: "Hello", user: User { name: "Taylor" } })]).await?;
/// # Ok(()) }
/// ```
pub use laravel_rust_model_macros::row;

/// Updates a model, or the rows of a model query, with struct syntax.
///
/// For a model, the macro returns the model's update builder, so you may chain `.quietly()`.
/// Awaiting the builder writes the columns that changed.
///
/// ```no_run
/// # use laravel::model::{Fill, model};
/// # use laravel::time::Timestamp;
/// # #[model] pub struct Post { pub id: i64, pub title: String, pub views: i64, pub published_at: Option<Timestamp> }
/// # #[derive(Fill)] #[fills(Post)] pub struct EditPost { pub title: String }
/// # async fn example(mut post: Post, input: EditPost) -> laravel::Result<()> {
/// use laravel::model::update;
///
/// update!(post { title: "New title" }).await?;
/// update!(post { published_at: laravel::now(), ..input }).await?;
/// update!(post { views += 1 }).quietly().await?;
/// # Ok(()) }
/// ```
///
/// `title: "New title"` calls `.title("New title")`, and `..input` calls `.fill(input)` before the
/// other fields. `..&input` calls `.fill(&input)`, which leaves `input` usable. `views += 1` and
/// `views -= 1` add and subtract in the database, as `views = views + 1`, like `increment` and
/// `decrement`.
///
/// After the update, the model holds the new values. If the write fails, the model keeps them,
/// and `refresh` reloads its row.
///
/// When you pass a model query instead of a model, the macro updates every row the query matches
/// without loading the rows, and returns the number of rows it changed. The fields work the same
/// way as for a model, and the update also sets `updated_at`. It runs no hooks and dispatches no
/// events:
///
/// ```no_run
/// # use laravel::model::{BelongsTo, HasMany, model};
/// # use laravel::time::Timestamp;
/// # #[model] pub struct User { pub id: i64, #[has_many] pub posts: HasMany<Post> }
/// # #[model] pub struct Post { pub id: i64, pub user_id: i64, pub views: i64, pub published_at: Option<Timestamp>, #[belongs_to] pub user: BelongsTo<User> }
/// # async fn example(user: User, editor: User) -> laravel::Result<()> {
/// use laravel::model::update;
///
/// let published: u64 = update!(user.posts().filter(Post::published_at.is_null()) {
///     published_at: laravel::now(),
/// })
/// .await?;
/// update!(Post::query().filter(Post::views.gt(1000)) { views -= 1, user: &editor }).await?;
/// # Ok(()) }
/// ```
///
/// As with a model, a misspelled column or a value of the wrong type fails to compile:
///
/// ```compile_fail
/// # use laravel::model::{model, update};
/// # #[model] pub struct Post { pub id: i64, pub views: i64 }
/// # async fn example() -> laravel::Result<()> {
/// update!(Post::query() { views: "many" }).await?;
/// # Ok(()) }
/// ```
///
/// ```compile_fail
/// # use laravel::model::{model, update};
/// # #[model] pub struct Post { pub id: i64, pub views: i64 }
/// # async fn example() -> laravel::Result<()> {
/// update!(Post::query() { veiws: 1 }).await?;
/// # Ok(()) }
/// ```
pub use laravel_rust_model_macros::update;

#[doc(hidden)]
pub use self::route::{
    __RouteScope, __Side, __bind, __find_by_route_key, __find_in_route, __route_field, __scope_owned, __scope_through,
};
pub use self::users::Users;
#[doc(hidden)]
pub use crate::queue::models::__RowIsGone;