how to get relation value in index in yii2

i have a relation in a model, in index it is displaying the id i want to show the name based on that foreign key record, in Yii.1 we just write relationName.Attribute and it display the value we want but how to do it with yii 2


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


            'fld_id',

            'fld_width',

            'fld_height',

            'fld_material_id',


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

        ],

    ]); ?>



in place of fld_material_id i want the name of that material in material table

First create relation in model file




public function getRelmaterial()

{

    	return $this->hasOne(model_name::className(), ['fld_material_id' => 'material_id']);

}

second, in modelSearch file

add this code


$query->joinWith(['relmaterial'']);

at last ,

use this relation like…





[

		      'label' => 'Material Name',

		      'attribute' => 'fld_material_id',

	              'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]

],



thanks i have another question if i want to show two attirbutes values in place of one like i want to show fld_name and fld_lastnmae what should i have to do

you can use this…

[code]

[

‘attribute’ => ‘Name’,

‘value’ => function ($data) {

			return $data->first_name." ".$data->last_name;

		},],

great but i need the data of the other model its considering it in the current model and giving the error Getting unknown property: app\models\Series::fld_width

define new object or render new object of another model at top of gridview or controller file

like ,

if $demo is another model object, so use it like


[ 'attribute' => 'Name',  'value' => function ($demo) {   

                              return $demo->first_name." ".$demo->last_name;    

                 	},],

i created the object before cgridView like this


$data = new Size();

but still its not working

can you give demo code for this error

Here it is Controller




public function actionIndex()

    {

        $searchModel = new Series_Search();

        $size		 = new Size();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'size' => $size,

            'dataProvider' => $dataProvider,

        ]);

    }



View




 <?= 

		GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'size' => $size,

        'columns' => [

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


            'fld_id',

            'fld_name',

            [

				'label' => 'Material Name',

				'attribute' => 'fld_material_id',

				'value' => 'fldMaterial.fld_name',


			],

            [

				'label' => 'Size',

                'attribute' => 'fld_size_id',

                // 'value' => 'fldSize.fld_width',

				'value' => function ($size) {

                                return $size->fld_width."x".$size->fld_height;

                        },

			],

            // 'fld_size_id',

        ],

    ]); ?>



i need fld_width and fld_height from size

the error is

remove $size from after $searchModel in view file

i did that now giving the error

use relation for getting another model value…

how can i use relation i tried on it but its not working

create relation that i describe already at post #2 ,follow that step and created it.

is this code included in yii code???




'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]



i thought size is written by mistake but its still giving error

can you describe above code … in detail

this is from your second post above i am asking is this a valid code or there is some typing mistake

i rewrite my previous code

e.g : city table

in model create relation like for getting state_name, state_code using state_id;




public function getRelState()

{

    	return $this->hasOne(State::className(), ['state_id' => 'city_state_id']);   //city_state_id is field of city

}

than, in modelSearch file add following code after $query = City::find();


$query->joinWith(['relState']);

last use it in index vews file…:




 [

      		'label' => 'State',

      		'attribute' => 'state_id',

              	'value' => 'relState.state_name',

     			OR

                  'value' => function ($model)      // $model is object of City

					{

					return $model->relState->state_name." ( ".$model->relState->state_code." ) ";

				},


 		],

yes i was missing the relation name its done thanks alot