How to render a related data's property instead of the ID?

How to render a related data’s property instead of the ID?

I have a relation in a model:




    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCreatedBy()

    {

        return $this->hasOne(User::className(), ['id' => 'created_by']);

    }



so the created_by is a foreign key on a User.

But in the details view I have no idea how I should render out the User’s name insated of the ID:




     <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'name',

            'procurer',

            'start_time',

            'end_time',

            'number',

            'style_hospitality:ntext',

            'style_setup:ntext',

            'demand_humanresource:ntext',

            'demand_technical:ntext',

            'shortscenario:ntext',

            'comment:ntext',

            'responsible_person',

            'form_date',

            'created_by',

        ],

    ]) ?>



Because this line renders out the "1" Id. But I would like to render out the "admin" username instead of the ID.

I have tried the ‘created_by.username’ but it does not work.

Is it possible? Or I should create a method for it?

createdBy.username ?

If it’s simple rendering then do as Federico Benedetti said. If not, then you can specify column as array:




[

    'attribute'=>'status',

    'value' => function ($model) {

        return $model->getStatusLabel();

    }

],