The Queen
The Queen

Reputation: 55

yii2 Basic - How to Display data in grid view column using id from other table?

Im new to yii2 framework. I never used this fremework before. It is good If someone can help me. Actually I already create a CRUD for 'Maintenance' table. Inside Maintenance table, the is one column named as 'Vehicle_ID' which is Foreign Key (FK) from 'Vehicle' table. The problem now is, the grid view for maintenance will display 'Vehicle_ID' as 1, but what I want is it to display the 'Plat_No' based on the id from 'Vehicle' table. Can someone please help me with these thing...

What should I change below in the grid view code?

<div class="box-body">

                    <h2 class="page-header">View Insurance # ID : <?php echo $model->Insurance_ID; ?></h2>

                    <?= DetailView::widget([
                        'model' => $model,
                        'attributes' => [
                            'Insurance_ID',
                            'Expiry_Date',
                            'Due_Date',
                            'Current_Coverage',
                            'Cover_Date_From',
                            'Cover_Date_To',
                            'Insurance_Provider',
                            'Policy_Cover_Note_No',
                            'NCD_Percent',
                            'Basic_Sum_Insured',
                            'Wind_Screen_Sum_Insured',
                            'Total_Premium_Price',
                            'Vehicle_ID', <!--Don't want this. How should I change here?-->
                        ],
                    ]) ?>

                </div>

Upvotes: 2

Views: 137

Answers (1)

Nabeel Javed
Nabeel Javed

Reputation: 89

In Maintenance Model class, you must have a relation created with Vehicle table. You can call Vehicle column by using reference of relation. Use Vehicle.Plat_No in grid. You can try using vehicle.Plat_No or Vehicle.Plat_No. Column name is case-sensitive.

<div class="box-body">

                    <h2 class="page-header">View Insurance # ID : <?php echo $model->Insurance_ID; ?></h2>

                    <?= DetailView::widget([
                        'model' => $model,
                        'attributes' => [
                            'Insurance_ID',
                            'Expiry_Date',
                            'Due_Date',
                            'Current_Coverage',
                            'Cover_Date_From',
                            'Cover_Date_To',
                            'Insurance_Provider',
                            'Policy_Cover_Note_No',
                            'NCD_Percent',
                            'Basic_Sum_Insured',
                            'Wind_Screen_Sum_Insured',
                            'Total_Premium_Price',
                            'Vehicle.Plat_No', <!--Don't want this. How should I change here?-->
                        ],
                    ]) ?>

                </div>

Relation will look something like

public function getVehicle()
{
    return $this->hasOne(Vehicle::className(), ['id' => 'Vehicle_id']);
}

If relation does not exists then please regenerate Model file using gii https://www.yiiframework.com/doc/guide/2.0/en/start-gii

Upvotes: 2

Related Questions