Show multiple values for a field in gridview

i am new in yii2. I have a languages master table (in name field contains html,css, java,php…) and a registration table in which there is a field called language_id. I have inserted multiple values for that field using json_encode.

Now in database showing language id’s as ["1","2","3","4","5"]

Now i want to display the data stored in db using a gridview. How can i show the multiple values for languages in gridview. Given below is what i have tried with my gridview




    <?= GridView::widget([

            'dataProvider' => $dataProvider,

            'filterModel' => $searchModel,

            'columns' => [

                ['class' => 'yii\grid\SerialColumn'],

    

                'first_name',

                'last_name',

                'gender',

                'address',

                ['label'=>'Course',

                'value'=>'course.name'],

                'email:email',

                ['label'=>'Languages Known',

                'value'=>'languages.name'],

                'course_completion_date',

    

                ['class' => 'yii\grid\ActionColumn'],

            ],

        ]); ?>

and in my Model code is as follows


  public function getLanguages(){

    

            $result = json_decode($this->language_id, true);

            

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

        }

This will not work as you expect it to. hasOne() method is not designed to be used like that.

You have to either 1) re-define your languages relation using a junction table, or 2) implement some custom method to convert a language id to a language name.

  1. Drop the ‘language_id’ column from the registration table. Create a junction table between registration and languages tables. Then establish a hasMany relation from Registration model to Language model using that junction table.

  2. Create a static method in Registration model that converts an id to a name of the language, or, from an array of ids to an array of names. Then you can call that method in your view for the registration.

thank you softark, I created junction table and now its working fine.