MorphTo class final

morphTo: the inverse of MorphOne / MorphMany. Each row names its owner's type in morphType and key in foreignKey, so rows of one table may point at different models (a comment on a post or a video).

morphName defaults to the name the relation is registered under, as Laravel defaults it to the method name. Stored types are resolved through types and then Relation.morphMap: there is no reflection to turn App\Models\Post into a model, so every possible target must be listed in one of the two.

Inheritance

Constructors

MorphTo({String? morphName, String? morphType, String? foreignKey, String? ownerKey, List<ModelDefinition<Model> Function()> types = const []})

Properties

foreignKey → String
The column holding the owner's key ({morphName}_id).
no setter
hashCode → int
The hash code for this object.
no setterinherited
knownTypes → List<ModelDefinition<Model>>
Every model this relation may point at: types, then the morph map.
no setter
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 setterinherited
morphName → String
no setter
morphType → String
The column holding the owner's morph class ({morphName}_type).
no setter
name ↔ String
Name under which the relation is registered (set by the definition).
getter/setter pairinherited
ownerKey → String?
The key of the target models (their primary key when null).
final
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 setteroverride
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
types → List<ModelDefinition<Model> Function()>
The models this relation may point at (besides Relation.morphMap).
final

Methods

associate(Model child, Model owner) → void
Points child at owner (no save).
correlated(ModelDefinition<Model> parent, DatabaseManager db) → ModelQuery<Model>
Not available: the owner table depends on each row. whereHas on a MorphTo goes through whereHasMorph instead.
override
correlatedTo(ModelDefinition<Model> type, DatabaseManager db, {required String parentTable}) → ModelQuery<Model>
A query on type correlated with the parent table (named parentTable), for whereHasMorph.
definitionFor(String type) → ModelDefinition<Model>
The model stored as type in morphType: its alias or, as Laravel still resolves rows written before an alias existed, its class name.
dissociate(Model child) → void
Clears the morph columns of child (no save).
eagerLoad(List<Model> parents, DatabaseManager db, [RelationConstraint? constraint]) → Future<void>
One query per stored type, as Laravel's MorphTo eager loads.
override
existenceQuery(ModelDefinition<Model> parent, DatabaseManager db, {required String parentTable}) → ModelQuery<Model>
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.
inherited
loadedModels(List<Model> parents) → List<Model>
The models loaded under name on parents, flattened (for nested eager loading).
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
queryFor(Model parent, DatabaseManager db) → ModelQuery<Model>
Query returning the related models of one parent.
override
toString() → String
A string representation of this object.
inherited

Operators

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