Relation<R extends Model> class abstract

Base relation (BLUEPRINT_V2 §7.5.4). Related definitions are resolved lazily through a thunk so two models may reference each other.

Implementers

Constructors

Relation(ModelDefinition<R> _related())
A relation to the model of the definition _related returns, read lazily so mutually referencing models can be declared.

Properties

hashCode → int
The hash code for this object.
no setterinherited
matchColumns → List<String>
The related rows' columns this relation matches them on (its foreign or owner key): added back when an eager load selects columns without them (with_('user:name') still reads users.id).
no setter
name ↔ String
Name under which the relation is registered (set by the definition).
getter/setter pair
parentColumns → List<String>
The parent rows' columns this relation reads (its local or foreign key): added to a parent query that selects columns without them, so the relation can still be loaded from its rows.
no setter
parentDefinition ↔ ModelDefinition<Model>?
The definition the relation is declared on (set by the definition); it names the default foreign key of HasMany and HasOne.
getter/setter pair
The related model's definition.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

correlated(ModelDefinition<Model> parent, DatabaseManager db) → ModelQuery<R>
A query on the related table correlated with the parent table, for whereHas / withCount.
eagerLoad(List<Model> parents, DatabaseManager db, [RelationConstraint? constraint]) → Future<void>
Loads the relation for many parents in one batched query and stores the result on each parent under name.
existenceQuery(ModelDefinition<Model> parent, DatabaseManager db, {required String parentTable}) → ModelQuery<R>
correlated against the parent table as parentTable names it (an alias inside a self-relation subquery). The built-in relations alias their own table when it has the parent's name (categories whose children are categories); custom relations fall back to correlated.
loadedModels(List<Model> parents) → List<Model>
The models loaded under name on parents, flattened (for nested eager loading).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
queryFor(Model parent, DatabaseManager db) → ModelQuery<R>
Query returning the related models of one parent.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

requiresMorphMap → bool
Whether models without a morph alias are refused (requireMorphMap).
no setter

Static Methods

chunk<T>(List<T> items, int size) → List<List<T>>
Splits items into lists of at most size, to keep IN lists within the database's limits.
enforceMorphMap(Map<String, ModelDefinition<Model> Function()> map, {bool merge = true}) → void
morphMap and requireMorphMap: every model used in a polymorphic relation must then have an alias.
keepColumns(QueryBuilder query, String table, Iterable<Object> columns) → void
Adds the columns that query (on table) does not select yet; bare names are qualified with table. A query that selects every column (none listed, *, table.*) is left alone.
morphClassOf(ModelDefinition<Model> definition) → String
The value stored in *_type columns for definition (Laravel's getMorphClass): its morph alias, else its ModelDefinition.morphClass.
morphedModel(String alias) → ModelDefinition<Model>?
The definition registered under alias (getMorphedModel), or null.
morphMap([Map<String, ModelDefinition<Model> Function()>? map, bool merge = true]) → Map<String, ModelDefinition<Model> Function()>
Registers morph aliases: a model listed here is stored in *_type columns under its alias ('post') instead of its ModelDefinition.morphClass, and MorphTo resolves the alias back. Returns the current map.
qualifySelect(QueryBuilder query, String table) → void
Qualifies the bare column names query selects with table: a query joining a pivot or intermediate table would find id ambiguous.
requireMorphMap([bool require = true]) → void
Whether models without a morph alias are refused (ClassMorphViolationException in Laravel).