I have this function in my controller:
public function actionResource()
{
$resourcegrporgs = Organization::find()->where(['groupid' => '1'])->all();
$resmembers = '';
foreach ($resourcegrporgs as $resourcegrporg) {
$resmembers = $resourcegrporg->getMembers()->all();
}
return $this->render('resource', [
'resourceMembers' => $resmembers,
'resourceGroups' => $resourcegrporgs,
]);
}
and in my view i try to get the members for each group as follows:
<?php foreach ($resourceGroups as $resourceGroup): ?>
<section class="team">
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-lg-12">
<h4 class="description"><?= $resourceGroup->name ?></h4>
<div class="row pt-md">
<?php foreach ($resourceMembers as $resourceMember): ?>
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12 profile">
<div class="img-box">
<img src=<?= $resourceMember->picture ?> class="img-responsive">
<ul class="text-center">
<a href="#"><li><i class="fa fa-facebook"></i></li></a>
<a href="#"><li><i class="fa fa-twitter"></i></li></a>
<a href="#"><li><i class="fa fa-linkedin"></i></li></a>
</ul>
</div>
<h1><?= Html::a(Html::encode($resourceMember->name),['memberdetail', 'id' => $resourceMember->id]) ?></h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php endforeach; ?>
And here is the organization model;
class Organization extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'organization';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'active', 'groupid'], 'required'],
[['active', 'groupid'], 'integer'],
[['lastdatemodified'], 'safe'],
[['name', 'logo', 'userid'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'logo' => 'Logo',
'active' => 'Active',
'userid' => 'Userid',
'lastdatemodified' => 'Lastdatemodified',
'groupid' => 'Groupid',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMembers()
{
return $this->hasMany(Member::className(), ['organizationid' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getGroup()
{
return $this->hasOne(Group::className(), ['id' => 'groupid']);
}
}
However, the foreach loop for the members is returning the members of the last group for every other group thus my question:
How can I fix it to render the members of each group individually?