yii2:data of related model in Gridview

Pawan

I have two models namely MedicineRequestEntry and MedicineRequest. MedicineRequestEntry is related to MedicineRequest via

public function getMedicineRequests()
    {
        return $this->hasMany(MedicineRequest::className(), 
['medicine_request_entry_id' => 'id']);
    } 

Now in grid-view of MedicineReuestEntry I am trying to pull the data from MedicineRequest Model using the relation using two alternative ways

like

[
           'attribute' => 'is_delivered',
            'value'=> 'medicineRequests.is_delivered'
        ],

In this method I am getting the value as not set. and another method:

[
               'attribute' => 'is_delivered',
               'value'=> '$data->medicineRequests->is_delivered'
            ],

In this method I am getting the error like:

Getting unknown property: app\models\MedicineRequestEntry::$data->medicineRequests->is_delivered

Now I need some help, what I am doing wrong here. Thank you.

deacs

You should use a callback function, see the guide:

[
    'value' => function ($data) {
        $str = '';
        foreach($data->medicineRequests as $request) {
            $str .= $request->is_delivered.',';
        }
        return $str;
    },
],

Or for the first result of the array:

[
    'value' => function ($data) {
        return $data->medicineRequests[0]->is_delivered;
    },
],

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using yii2's gridview with a normal array of data

Using Yii2 with array of data and a Gridview with sorting and filter

Sort and filter data in GridView Yii2 where column is not in database

Filter setup for related model in GridView

Yii2 Display Data from the Database Without Using GridView

Yii2 Select only few columns from related model

displaying multi related data in yii2

is it possible to set label value from related model in datacolumn in gridview in yii2

Yii2 GridView value call to a model function

Yii2 Grid view syntax to access data from related model

Yii2 data-* attributes not rendered using GridView Column

GridView filter model in Yii2

How to get previous and next row model ID in yii2 gridview data row

How to call model function from Yii2 Gridview value

Yii2 GridView widget not displaying data

Yii2 - form and related gridview on the same page

Yii2 Gridview data not changing in Pjax

Data not saving through model in yii2

yii2 change gridview model

Yii2 gridview relation data search for all column

Yii2 filter related model

How to get data from not directly related tables and use it on gridview with modelSearch() Yii2

Yii2 Display Related Data not in Array, not using Listview or Gridview

In Yii2 Unable to show custom column from model in gridview

Pjax Gridview not displaying data in yii2 advanced

Yii2 Gridview table showing Data

Yii2 gridview how to get related model only once for more attributes

Export data to excel using Data from Gridview Yii2

Yii2 model to exclude related records