Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve other parameter instead of id in gridview and detailview yii2?

In columns Role, Position, Department you see ids of these db tables "role","position","department". GridView

How can I paste names instead of ids?

Thanks in advance.

like image 868
Vitaly Park Avatar asked Jan 21 '26 19:01

Vitaly Park


1 Answers

For Detail view check the relation in model and using the relation you do like this:-

'attributes' => [
    'title',             
    'relational_data',  
    [                    
        'label' => 'Field Name',
        'value' => $data->relation_table->field_name,
    ],        
],

For Grid view:-

[
    'attribute' => 'role_id',
    'value'     => function ($data) {
        return $data->relation_table->field_name;
    },
],

And your Table relation should be like this:-

public function getRelation_table()
{
    return $this->hasOne(Table::className(), ['relation_id' => 'id']);
}
like image 141
Rajkumar R Avatar answered Jan 23 '26 08:01

Rajkumar R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!