Model class abstract

Core Active Record implementation serving as the bridge between Dart objects and the DB.

Aggregates feature mixins (Events, Casts, Relations) and manages the persistence lifecycle, including dirty checking and attribute synchronization. Concrete classes need only define the table and the fromMap hydration factory.

Mixed-in types
Implementers
Available extensions

Constructors

Model([Map<String, dynamic> rawAttributes = const {}])

Properties

attributes Map<String, dynamic>
The raw data source for the model. Modified by setters/casts, read by getters/DB.
getter/setter pair
casts Map<String, dynamic>
Configuration map defining how to cast specific attributes.
no setterinherited
columns List<SchemaColumn>
Defines the schema columns for the model.
no setterinherited
exists bool
Indicates if the model currently exists in the database (persisted). This change logic for CREATE or UPDATE
getter/setter pair
fillable List<String>
Attributes explicitly allowed for bulk assignment (whitelist).
no setterinherited
guarded List<String>
Attributes blocked from bulk assignment (blacklist).
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
id ↔ dynamic
The primary key value, required to bind relation queries to this specific instance.
getter/setter pair
original Map<String, dynamic>
A snapshot of attributes at the time of hydration or last save. Used to calculate diffs for efficient UPDATE queries.
getter/setter pair
pivot Pivot?
Holds the intermediate table data for Many-to-Many relationships.
getter/setter pair
primaryKey String
The primary key column name (usually 'id').
no setter
relations Map<String, dynamic>
Container for eager-loaded data (e.g., user.relations['posts']).
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
table String
The table name of the current model, used to generate default Foreign Keys (e.g., user_id).
no setter

Methods

belongsTo<R extends Model>(R creator(Map<String, dynamic>), {String? foreignKey, String? ownerKey}) BelongsTo<R>
Defines an inverse 1:1 or N:1 relationship where the Foreign Key resides on this model.
inherited
belongsToMany<R extends Model>(R creator(Map<String, dynamic>), String pivotTable, {String? foreignPivotKey, String? relatedPivotKey}) BelongsToMany<R>
Defines a N:N relationship via an intermediate pivot table.
inherited
boolean(String key) bool?
inherited
date(String key) DateTime?
inherited
dehydrateAttributes() Map<String, dynamic>
inherited
delete() Future<void>
Permanently removes the record (unless HasSoftDeletes overrides this).
doubleNum(String key) double?
inherited
enumeration<T extends Enum>(String key, List<T> values) → T?
Resolves the raw value at key to a specific Enum entry within values.
inherited
fill(Map<String, dynamic> attributes) → void
Safely hydrates the model from attributes, filtering out forbidden keys.
inherited
forceFill(Map<String, dynamic> attributes) → void
Bypasses all security guards to set attributes.
inherited
fromMap(Map<String, dynamic> map) Model
Factory method to hydrate a concrete instance from a DB row (Map).
getAttribute<T>(String key) → T?
Safe accessor that transforms raw data into type T based on casts.
inherited
getDirty() Map<String, dynamic>
Returns a map of attributes that have changed since the last sync.
getEnum<T extends Enum>(String key, List<T> values) → T?
Maps a raw value to a specific Enum entry.
inherited
getPivot<T extends Pivot>() → T?
Helper to safely cast the pivot object to a specific type.
getRelated<T>(String name) → T?

Available on Model, provided by the TypedRelations extension

Safely retrieves a single related model (1:1 or N:1).
getRelation(String name) Relation<Model>?
Accessor for hydration logic to retrieve cached/eager-loaded data.
inherited
getRelationList<T>(String name) List<T>

Available on Model, provided by the TypedRelations extension

Safely retrieves a list of related models (1:N or N:N).
hasMany<R extends Model>(R creator(Map<String, dynamic>), {String? foreignKey, String? localKey}) HasMany<R>
Defines a 1:N relationship where the Foreign Key resides on the related model R.
inherited
hasManyThrough<R extends Model, I extends Model>(R relatedCreator(Map<String, dynamic>), I intermediateCreator(Map<String, dynamic>), {String? firstKey, String? secondKey}) HasManyThrough<R, I>
Defines a distant 1:N relationship through an intermediate model.
inherited
hasManyThroughPolymorphic<R extends Model, I extends Model>(R relatedCreator(Map<String, dynamic>), I intermediateCreator(Map<String, dynamic>), {required String name, required String type, String? firstKey, String? secondKey}) HasManyThrough<R, I>
Defines a distant 1:N relationship where the target model is polymorphic.
inherited
hasOne<R extends Model>(R creator(Map<String, dynamic>), {String? foreignKey, String? localKey}) HasOne<R>
Defines a 1:1 relationship where the Foreign Key resides on the related model R.
inherited
hydrateAttributes(Map<String, dynamic> rawData) → void
inherited
integer(String key) int?
inherited
isDirty([String? attribute]) bool
Determines if the model or a specific attribute has been modified.
isFillable(String key) bool
Determines if key is safe to write.
inherited
json<T>(String key) → T?
Helper for retrieving nested structures (e.g., Map or List) cast to T.
inherited
morphMany<R extends Model>(R creator(Map<String, dynamic>), String name) MorphMany<R>
Polymorphic 1:N. The target model R stores the _id and _type.
inherited
morphOne<R extends Model>(R creator(Map<String, dynamic>), String name) MorphOne<R>
Polymorphic 1:1. Similar to morphMany but enforces a single result.
inherited
morphToMany<R extends Model>(R creator(Map<String, dynamic>), String name) MorphToMany<R>
Polymorphic N:N.
inherited
morphToTyped(String name, Map<String, Model Function(Map<String, dynamic>)> typeMap) MorphTo<Model>
The inverse of a polymorphic relationship (the child side).
inherited
newInstance() Model
newQuery() QueryBuilder<Model>
Entry point for the Fluent Query Builder.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDeleted() Future<void>
Post-removal hook.
inherited
onDeleting() Future<bool>
Pre-removal hook. Return false to prevent the Delete operation.
inherited
onSaved() Future<void>
Post-persistence hook invoked after the record is committed.
inherited
onSaving() Future<bool>
Pre-persistence hook. Return false to abort the Save operation.
inherited
query() QueryBuilder<T>

Available on T, provided by the TypedQuery extension

Bootstraps the builder chain.
refresh() Future<void>
registerGlobalScopes(QueryBuilder<Model> builder) → void
save() Future<void>
Persists the model to storage (Upsert logic).
setAttribute(String key, dynamic value) → void
Writes data to attributes, applying transformations for persistence.
inherited
string(String key) String?
inherited
syncOriginal() → void
Snapshots current attributes to original.
toString() String
A string representation of this object.
inherited
where(String column, dynamic value) QueryBuilder<Model>
withRelations(List<String> rels) QueryBuilder<Model>

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](String key) → dynamic
Exposes dynamic access, useful for serialization loops or form field binding.
inherited
operator []=(String key, dynamic value) → void
inherited