php - is it possible to set label value from related model in datacolumn in gridview in yii2 -


related question have searched alot on internet didnot find answer question.

i try accomplish simple task in gridview in yii2, details follow.

<?= gridview::widget([     'dataprovider' => $dataprovider,     'filtermodel' => $searchmodel,     'columns' => [         ['class' => 'yii\grid\serialcolumn'],         'id',         'name',         'email:email',         'phone',         'age',         [             'class' => yii\grid\datacolumn::classname(),             'label' =>'custom',             'attribute' => 'cusid',             'format'=>'html',             'value' => function ($model) {                 return html::a($model->cus->key, ['customobj/view', 'id' => $model->cusid],['class' => 'btn btn-success']);             },             'filter' => yii\helpers\arrayhelper::map(app\models\customobj::find()->all(), 'id', 'key')         ],         ['class' => 'yii\grid\actioncolumn'],     ], ]); ?> 

the above code working fine. want make lable dynamic related model in following.

[         'class' => yii\grid\datacolumn::classname(),         'label' =>'custom',         'attribute' => 'cusid',         'format'=>'html',         'value' => function ($model) {             return html::a($model->cus->key, ['customobj/view', 'id' => $model->cusid],['class' => 'btn btn-success']);         },         'filter' => yii\helpers\arrayhelper::map(app\models\customobj::find()->all(), 'id', 'key')     ], 

i have tried below code gives

blockquote htmlspecialchars() expects parameter 1 string, object given blockquote

[             'class' => yii\grid\datacolumn::classname(),             'label' => function ($model) {                 return $model->cus->key;             },             'attribute' => 'cusid',             'format'=>'html',             'value' => function ($model) {                 return html::a($model->cus->key, ['customobj/view', 'id' => $model->cusid],['class' => 'btn btn-success']);             },             'filter' => yii\helpers\arrayhelper::map(app\models\customobj::find()->all(), 'id', 'key')         ], 

i using yii2 version. highly appreciated

try using header

$varheader = yourfuntion();  [         'class' => yii\grid\datacolumn::classname(),         'header' =>  $varheader ,         'attribute' => 'cusid',         'format'=>'html',         'value' => function ($model) {             return html::a($model->cus->key, ['customobj/view', 'id' => $model->cusid],['class' => 'btn btn-success']);         },         'filter' => yii\helpers\arrayhelper::map(app\models\customobj::find()->all(), 'id', 'key')     ], 

Comments