user2511599
user2511599

Reputation: 852

Yii2 get related data in Model by a hasMany relation

In Model, when I have a hasMany relation, can I somehow get the values of a certain attribute concatenated? Is there something like that?

public function getRelatedManyAttribute() {
    return $this->relatedMany->concat('attributeMany');
}

Upvotes: 1

Views: 1557

Answers (2)

PLM57
PLM57

Reputation: 1296

I am not entirely sure if I understood you cerrectly, but if you want all the values of the same attribute of a 1:n-relation it could be this simple oneliner:

public function getIdsConcatenated() {
    return implode(', ', ArrayHelper::getColumn($this->myManyRelation, 'id'));
}

Upvotes: 1

rahul s negi
rahul s negi

Reputation: 139

I think you are looking for this -

  public function getSubMenuMenu()
{
     return $this->hasMany(Your_table_name::className(), ['id' => 'your_column_name']);;
}

for more understanding visit this link

Upvotes: 0

Related Questions