Expand description
Eloquent-style attribute casts for laravel-rust models.
laravel-rust models have typed fields and no cast hook, so a cast is the field’s type, the way
Encrypted<T> is. get is Decode + Serialize, set is From<&T> for Value + Deserialize.
ⓘ
#[model]
struct Invoice {
id: i64,
issued_at: DateCast, // DateCast::class
paid_at: Option<DateTimeCast<Parsed>>, // DateTimeCast::SETPARSED
period: Option<DateCast<MonthYear>>, // DateCast::SETONLYMONTHYEAR
}A cast of your own implements ColumnCast. Casts chosen at run time, by name, are in laravel_casts::dynamic,
behind the dynamic feature (off by default).
Modules§
- canonical
- A date cast stored as data, not shown to people: in a JSON column, a cache or a queued job.
- dynamic
dynamic - Casts chosen at run time, by name, over JSON values, as a
cast_array()helper over Eloquent casts does in PHP.
Macros§
- casts
dynamic - A list of
(field, cast)where each cast is a text or an object, without wrapping each one in aSpec. - date_
format - Declare a
Formatwith its own pattern, then use it as the cast’s type parameter. - impl_
column_ cast - Gives a type that implements
ColumnCastthe traits alaravel-rustmodel field asks for:Decode,Nullable, andFrom<T>,From<&T>andFrom<&&T>forValue.
Structs§
- Cast
- A date column that reads with its storage format, writes the same, and serializes with the
configured display pattern. Use
DateCastandDateTimeCastas field types. - Configured
DateCast::class: serialize with the configured pattern, accept the storage format.- Date
AndTime DATETIMEcolumn.- Date
Config - Where the date patterns of an application are configured: one per kind, one per purpose, and two legacy settings used when nothing else is set.
- Date
Only DATEcolumn.- Date
Patterns - One pattern per kind.
- Month
Year DateCast::SETONLYMONTHYEAR: read and writem/Y.- Parsed
DateCast::SETPARSED: also accept the configured pattern.- PhpDate
Pattern - A compiled PHP date pattern.
Enums§
- Cast
Error - A value that cannot be cast, or a cast that cannot be understood.
- Error
- A date pattern that cannot be used.
- Kind
- Which column type a cast stores.
Traits§
- Column
Cast - A model field stored as another type: the column holds
ColumnCast::Repr, the program works withSelf. - Format
- Marker for the cast’s constructor arguments. Equivalent to the PHP
Cast::class . ':true,m/Y,m/Y'string, as a type. - Storage
- Marker for the stored column type. Equivalent to the PHP subclass (
DateCast,DateTimeCast).
Functions§
- configure
- Replace the settings. Patterns are validated first, so a bad one fails at boot, not on a row.
Type Aliases§
- Date
Cast Casts\DateCast: aDATEcolumn.DateCast<Parsed>isDateCast::SETPARSED,DateCast<MonthYear>isDateCast::SETONLYMONTHYEAR.- Date
Time Cast Casts\DateTimeCast: aDATETIMEcolumn.- Result
- A date pattern that failed validation.