Question: i want add a column in GridView::widget

```
 <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'owner_id',
            'situation',
            'address',
              [
//            'attribute' => 'address',
            'format' => 'html',
            'label' => 'Image',
            'value' => function ($data) {
                return Html::img('http://iicity.ir/' . $data['address'],
                    ['width' => '60px']);
            },
        ],

            'slideshow_text',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

```

i want add name of ***'owner\_id',*** from another table 
name is in ( account )table 
table :

id=1 name=jack

id=2 name=sara

Answer: 
*@suibber's* accepted answer:

```
[
    'attribute' => 'owner_id',
    'value'=>function ($model) {
        $info = Account::findOne(['id' => $model->owner_id]);

        return isset($info->name) ? $info->name : $model->owner_id;
    },
],

```

has some problems:

**1)** Constructing SQL queries logic is exposed in view, so MVC princple is violated. View is only for displaying data.

**2)** All related owner's data is lazily loaded, that means amount of queries is multiplied by number of displayed rows in the table.

**3)** Why displaying `owner_id` if the record doesn't exist? That simply means the data is corrupted. You should not even check owner for existence here because logically it should always exist, you need to perform validation before saving and don't delete data with manually written SQL queries (or using GUI tools) in case of missing foreign keys.

**4)** Lack of autocompletion for IDE.

The Yii2 way of solving this problem is obviously using relations.

Add this to your model:

```
<?php

namespace app\models;

use app\models\User;
use yii\db\ActiveRecord;

/**
 * ...
 *
 * @property User $owner
 */
class Picture extends ActiveRecord
{
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOwner()
    {
        return $this->hasOne(User::className(), ['id' => 'owner_id']);
    }
}

```

Then to display owner name in `GridView`:

```
[
    'attribute' => 'owner_id',
    'value'=>function ($model) {        
        /* @var $model app\models\Picture */

        return $model->owner->name;
    },
],

```

Or just:

```
'owner.name',

```

The advandtages are clear - it's framework way, code is shorter, more readable, data can be eagerly loaded using `->with('owner')` in your `ActiveQuery` for `GridView`.

By the way, no `INNER JOIN` is needed. Related data is fetched with separate SQL query. If, for some reason, owner is optional and can be not existing in some records, use:

```
[
    'attribute' => 'owner_id',
    'value'=>function ($model) {        
        /* @var $model app\models\YourModel */

        return $model->owner ? $model->owner->name : null;
    },
],

```

Working with related data is well covered in [official docs](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data), so it's pretty basic question. Check the docs first.
```
use app\models\Account;

...

[
    'attribute' => 'owner_id',
    'value' => function ($model) {
        $info = Account::findOne(['id' => $model->owner_id]);

        return isset($info->name) ? $info->name : $model->owner_id;
    },
],

```