[SOLVED] GridView - display value in column from relation


I’ve been playing with Yii-1 back in the days. Then I had a break for long time. And now trying to write simple CRUD app with Yii-2.

My problem is to display value from relation in view. I’ve searched forums and haven’t found solution.

I have a model and relation defined in it to access data from another table.

In Yii-1 I was accessing the relation data



		'header'=>'Manager Type',



Where rel_id_manager_type was relation defined in master model. And name_en was column name in related table.

In Yii-2 I am trying to access relation data:


             'attribute'=>'Business Type',


             'value' => Html::encode($data->businessTypes->name_en),


But it returns

Undefined variable: data

What am I doing wrong?

OK got it. Simply:


Where businessTypes is relation to another table and name_en is column name with desired value.

Or, if you want to be more complex, you can use like this:

'value' => function ($model, $key, $index, $column) {

    return ($model->supAmount == 0 ? null : $model->transaction->transactionsTotal);