Retrieve Intermediate and Pivot Data
mainUse withIntermediate() to retrieve attributes from intermediate models or tables.
- Basic usage:
->withIntermediate(Model::class)retrieves all columns. - Specific columns:
->withIntermediate(Model::class, ['col1', 'col2']). - Nested accessors: You can nest accessors to reach multiple levels, e.g.,
->withIntermediate(Post::class)->withIntermediate(User::class, ['*'], 'post.user').
For BelongsToMany or MorphToMany relationships, use withPivot() to access pivot table data.
Pivot usage: ->withPivot('pivot_table', ['column']).
// Accessing intermediate model data
public function comments(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep
{
return $this->hasManyDeep(Comment::class, [User::class, Post::class])
->withIntermediate(Post::class, ['id', 'title'], 'accessor');
}
// Accessing pivot data
public function permissions(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep
{
return $this->hasManyDeep(Permission::class, ['role_user', Role::class])
->withPivot('role_user', ['expires_at']);
}