显示名称而不是ID - YII2

In my form I have the category_id, In the drop-down, it displays the name but after saving it displays the ID, how do I display the name instead of the ID?

<?= $form->field($model, 'category_id')->dropDownlist(
     ArrayHelper::map(Category::find()->all(), 'id', 'category_name'),
     [
         'prompt' => 'Select Category',
         'onchange' => '$.post( "index.php?r=sub-cat/lists&id='.'"+$(this).val(), function( data ) {
                $( "select#tickets-sub_category" ).html( data );
          });'
     ]); 
?>

What I have tried so far in the view is this:

[
   'label' => $model->category_id->getAttributeLabel('category_name'),
   'value' => $model->category_id->category_name
]

But I'm getting an error: Call to a member function getAttributeLabel() on integer

please try this code in your grid-view.

['label'=>'Category Name',
'value' => function ($data) {
     return Category::findOne(['id'=>$data->category_id])->category_name;
},]

Or try this code if you want to display category name instead of id anywhere on the view Assuming that you have two tables 1. Catgeory (id, name) 2. Post (id, category, title) In your Post model

public function getCategoryId()
    {
        return $this->hasOne(\path\to\models\Category::className(), ['id' 
        => 'category']);
    }
/**
 * @getCategoryName
 *
 */
public function getCategoryName()
{
    return $this->categoryId ? $this->categoryId->name : '- no category -';
}

Now you can use categoryName ?> anywhere on your view

In case if you are using GridView Just change attribute to categoryName