Base for Hybrid Active Record models (ADR-006): an attribute bag with
typed accessors written by the developer (or dartisan make:model),
dirty tracking, casts, timestamps, soft deletes, relations and events.
final class User extends Model {
static final modelDefinition = ModelDefinition<User>(table: 'users', factory: User.new, ...);
@override
ModelDefinition<User> get modelDefinition => User.modelDefinition;
static ModelQuery<User> query() => modelDefinition.query();
int get id => getAttr('id');
String get name => getAttr('name');
set name(String value) => setAttr('name', value);
}
- Implemented types
- Implementers
- Available extensions
Constructors
- Model()
Properties
-
attributes
→ Map<
String, Object?> -
Raw stored attributes (after
setcasts).no setter - database → DatabaseManager
-
The database manager this model was loaded through (or the container's).
no setter
- exists ↔ bool
-
Whether the model is persisted.
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Object?
-
Primary key value.
no setter
-
modelDefinition
→ ModelDefinition<
Model> -
Static metadata; override as
ModelDefinition<User> get modelDefinition.no setter -
original
→ Map<
String, Object?> -
Attributes as last synced with the database.
no setter
-
pivot
↔ Map<
String, Object?> ? -
Pivot columns when loaded through a
BelongsToManyrelation.getter/setter pair -
relations
→ Map<
String, Object?> -
Loaded relations by name.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- table → String
-
no setter
- trashed → bool
-
Whether the model is soft deleted.
no setter
- wasRecentlyCreated ↔ bool
-
Whether the model was inserted during the current request/operation.
getter/setter pair
Methods
-
append(
List< String> attributes) → Model -
attach(
String name, Object ids, {Map< String, Object?> pivot = const {}}) → Future<void> -
Attaches related through a
BelongsToManyrelationname. -
attachDatabase(
DatabaseManager? db) → void - Attaches a database manager (used by queries and tests).
-
decrement(
String column, [num amount = 1, Map< String, Object?> extra = const {}]) → Future<int> -
delete(
) → Future< bool> - Deletes (soft deletes when configured).
-
detach(
String name, [Object? ids]) → Future< int> -
Detaches
ids(or all) through aBelongsToManyrelationname. -
fill(
Map< String, Object?> attributes) → Model -
Mass assigns
attributeshonouringfillable/guarded. -
fireEvent(
ModelEvent event) → Future< bool> -
Fires
event(used by queries forretrieved). -
forceDelete(
) → Future< bool> - Permanently deletes a soft-deletable model.
-
forceFill(
Map< String, Object?> attributes) → Model -
Mass assigns
attributesignoringfillable/guarded. -
fresh(
) → Future< Model?> -
A fresh copy from the database (or
nullwhen gone). -
getAttr<
R> (String key) → R - Typed attribute read with casts and accessors applied.
-
getChanges(
) → Map< String, Object?> -
The attributes the last
save()wrote (Laravel'sgetChanges()): what anupdatedorcreatedobserver sees changed, since by then original already matches attributes. -
getDirty(
) → Map< String, Object?> - Attributes that changed since the last sync.
-
hasAttribute(
String key) → bool -
Whether
keyhas a value in the attribute bag. -
hydrateFrom(
Map< String, Object?> row, {DatabaseManager? db}) → void - Replaces every attribute from a database row. Called by the query.
-
increment(
String column, [num amount = 1, Map< String, Object?> extra = const {}]) → Future<int> -
Atomically increments
columnin the database and locally. -
isClean(
[String? key]) → bool -
isDirty(
[String? key]) → bool -
Whether
key(or any attribute) differs from the database. -
isNot(
Model other) → bool -
isSameAs(
Model other) → bool -
Whether
otheris the same row. -
load(
Object relations) → Future< Model> -
Loads
relations(list or map with constraints) onto this model. -
loadMissing(
List< String> relations) → Future<Model> - Loads the relations that are not loaded yet.
-
makeHidden(
List< String> attributes) → Model -
makeVisible(
List< String> attributes) → Model -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifications(
{bool unreadOnly = false}) → Future< List< DatabaseNotification> > -
Available on Model, provided by the NotifiableModel extension
This model's stored notifications, newest first. -
notify(
Notification notification) → Future< void> -
Available on Model, provided by the NotifiableModel extension
-
refresh(
) → Future< Model> - Reloads attributes (and drops loaded relations) from the database.
-
A loaded relation (
List<Post>/ModelCollection<Post>for *-many, the model ornullfor *-one). Throws when not loaded and preventLazyLoading is on. -
A query scoped to the relation
name(user.relatedQuery<Post>('posts')). -
relationLoaded(
String name) → bool -
Whether
namewas loaded. -
replicate(
{List< String> except = const []}) → Model -
Copy without key and timestamps (Laravel
replicate). -
restore(
) → Future< bool> - Restores a soft-deleted model.
-
save(
) → Future< bool> -
Inserts or updates the model. Returns
falsewhen an observer cancels. -
saveQuietly(
) → Future< bool> - Saves without firing events.
-
setAttr(
String key, Object? value) → void - Attribute write with mutators and casts applied.
-
setRelation(
String name, Object? value) → void - Stores a loaded relation result.
-
sync(
String name, Object ids, {bool detaching = true}) → Future< Map< String, List< >Object?> > -
Syncs pivot rows through a
BelongsToManyrelationname. -
syncOriginal(
) → void - Marks the current attributes as the persisted state.
-
toJson(
) → Map< String, Object?> -
JSON representation: visible attributes, appends and loaded relations
(relation names snake_cased as given), in the order Laravel 8 to 13
write them (
HasAttributes::attributesToArray): an append that names an existing attribute replaces it in place, and a Cast.dateTimeFormat attribute is written in its format, in UTC.override -
toMap(
) → Map< String, Object?> - Attributes with casts applied (no hidden filtering).
-
toResponse(
Request request) → Response -
override
-
toString(
) → String -
A string representation of this object.
override
-
touch(
) → Future< bool> -
Updates the
updated_attimestamp. -
update(
Map< String, Object?> attributes) → Future<bool> - Mass assigns and saves.
-
wasChanged(
[String? key]) → bool -
Attributes changed by the last
save(). -
withoutEvents<
R> (Future< R> body()) → Future<R> -
Runs
bodywith model events disabled.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
String key) → Object? -
operator []=(
String key, Object? value) → void
Static Properties
- preventLazyLoading ↔ bool
-
When true, accessing a relation that was not eager loaded throws
instead of querying lazily (catches N+1 problems in tests).
getter/setter pair
- preventSilentlyDiscardingAttributes ↔ bool
-
When true, assigning a guarded attribute throws instead of being
silently dropped (recommended outside production).
getter/setter pair