belongsToThrough
relationship is the one
side of a many-to-one
relationship with an intermediate entity. It is used when you want to access a related, owning entity through one or more intermediate entities. For instance, a Post
may belong to a Team
via a User
.belongsToThrough
is an array of relationship function names to walk through to get to the related entity. The first relationship function name in the array must exist on the current entity. Each subsequent function name must exist on the related entity of the previous relationship result. For our previous example, author
must be a relationship function on Post
. team
must then be a relationship function on the related entity resulting from calling Post.author()
. This returns a belongsTo
relationship where the related entity is User
. So, User
must have a team
relationship function. That is the end of the relationship function names array, so the related entity resulting from calling User.team()
is our final entity which is Team
.HasOneThrough
relationships can be configured to return a default entity if no entity is found. This is done by calling withDefault
on the relationship object.withDefault
.true