ExpressionEngine Docs

Working with Relationships

Accessing Related Models

Related models are available on the model directly using the relationship name. They are easily differentiated by their CamelCase naming conventions:

$group = $member->PrimaryRole;

Note: If you are filtering on relationship the related models will be limited to those that match the criteria specified. If you need full set of related models, you would need to eager load the relationship to be populated before eager loading the relationship for the filter, which will need to be aliased. $entry = ee('Model')->get('ChannelEntry')->with('Categories', 'Categories as RelatedCategory')->filter('RelatedCategory.cat_id', 7)->all();

Setting Relationships

To set a relationship, simply assign the model or collection that you want to relate, and then save() to commit the change:

$member->PrimaryRole = ee('Model')->get('Role', 1)->first();

Adding Related Models

To add additional models to a HasMany or HasAndBelongsToMany relationship, simply use PHP’s array syntax, or call add() with a model to add:

$member_group[] = $member;

Removing Related Models

To unset a relationship, simply assign NULL:

$member->PrimaryRole = NULL;

Caution: Unsetting a relationship may result in the deletion of the child, where it would otherwise be orphaned. This can be prevented by establishing a new relationship before calling save().

To remove a related model from a collection, call remove() with the related model or its primary key value:

$member_group->remove(5); // remove member_id 5