Access variable in Gridview function yii2

yii2 gridview filter
yii2 gridview dataprovider
yii2 gridview row options
yii2 listview vs gridview
yii2 gridview custom column
yii2 display data from database
yii2 gridview visible condition
yii2 gridview relational data

I am trying to access a variable on view file into Gridview but it is throwing error that it should be array but null given i am declaring $totalDays on top of my view file and i am using it in Gridview like below

[
            'attribute' =>  'class_id',
            'format' => 'raw',
            'label' => "Class Date",
            'value' => function ($model) {
                array_push($totalDays,$model->class->date);
                return $model->class->date;
            },
            'footer'=> '<span>Total Days</span>',
],

But it throws following error

array_push() expects parameter 1 to be array, null given


To explain, $totalDays is not available in the widget because the whole function only gets run when the widget is rendered, $totalDays is no longer declared. As @arogachev hinted above, you will need to make $totalDays in your model, then you can access it. Try this in your model;

public function getTotalDays(){
//Your logic here to generate totalDays
return $totalDays;
}

Then you can use it in your view like this;

[
    'attribute' =>  'class_id',
    'format' => 'raw',
    'label' => "Class Date",
    'value' => function ($model) {
        array_push($model->totalDays, totalDays,$model->class->date);
        return $model->class->date;
    },
    'footer'=> '<span>Total Days</span>',
],

How To Access Variable Passed From Controller Into , How To Access Variable Passed From Controller Into Cgridview · Yii 1.1 mentioned that the project is running on PHP 5.1 hence anonymous functions are not supported. It has a property 'grid' that means the parent grid. Here’s a handy tip on how to pass variables to the custom code in Gridview for raw formatted rows. I’m embarrassed that I’d never seen this before. I’d never the used the syntax and never previously been able to find out how to do this.


Insert use after function signature in value callable function:

[
            'attribute' =>  'class_id',
            'format' => 'raw',
            'label' => "Class Date",
            'value' => function ($model) use($totaleDays) {
                array_push($totalDays,$model->class->date);
                return $model->class->date;
            },
            'footer'=> '<span>Total Days</span>',
],

Displaying Data: Data Widgets | The Definitive Guide to Yii 2.0, CListView; CGridView; Passing your own variables in your controller $this->​widget('SpecialGridView', array( 'dataProvider' => $dataProvider, 'extraparam' => 1234 // your special parameter and then you can access 'switch' variable in _view.php like this: Then you can use an anonymous function. We are then passing the searchModel and dataProvider variables to the render function, which passes it to the view for access by the GridView Widget. 12.5.2 The Search Model. Let's take a look at the actual search model to see what is happening. Here is the search function in CustomersSearch.php under models:


While you can definetely pass this variable through use section of closure, it's wrong in your code. MVC principle is violated, because view it's only for display, and you exposing logic such as array_push.

I'd recommend to refactor it and place data calculation in model, so you can simply call return $model->yourMethod(); and it will return desired data.

Special $variables in CGridView and CListView | Wiki, The key part is “function ($model) use ($custom_variable) {}” where $​custom_variable is any variable name or names that you choose. I believe  GridView ¶. Data grid or GridView is one of the most powerful Yii widgets. It is extremely useful if you need to quickly build the admin section of the system. It takes data from a data provider and renders each row using a set of columns presenting data in the form of a table.


As @Fabrizio said. Insert use after function signature in value callable function:

'value' => function ($model) use($totaleDays) {
    array_push($totalDays,$model->class->date);
    return $model->class->date;
},

For multiple value to pass in to value function you can use as below.

'value' => function ($model) use($var1, $var2....., $varN) {
    array_push($totalDays,$model->class->date);
    return $model->class->date;
},

Like this use($var1, $var2....., $varN)

Tip on Passing Variable to Raw Custom Grid View Row, Data grid or GridView is one of the most powerful Yii widgets. this function in the controller to get the dataProvider for the GridView: yii\filters\AccessControl: beforeFilter() yii\base\ActionFilter: canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject: canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject: className() Returns the fully qualified name of this class. yii\base\BaseObject: detach() {@inheritdoc}


Output data widgets, I have a function in my Teacher model which returns categories array. getCaterogies() it doesn't take. How can I get this working for gridview? Yii2: Passing index page to breadcrumbs in view and update actions Tag: gridview , yii2 , breadcrumbs , yii2-advanced-app 1) Have several pages of results in index (GridView) with default LinkPager settings.


Yii gridview use outside variable in value, i needs to pass some custom variables in gridview to process/show in http://​php.net/manual/en/functions.anonymous.php#example-186  I am new to Yii2 environment. I have a case in which I want to save some variables in session before the form is submitted. By reading Session Handling In Yii Framework 2.0 I came to know how to save them like


Extraparams (additional variables in widget) in gridview? · Issue , To access session variables, you may use set() and get() methods. Step 3 − Add an actionAccessSession method to the SiteController. public function  Allan Jensen on (draft) Understanding Yii 3 2 days ago; Szabó, Zoltán on Using Yii with Nginx and PHP-FPM a month ago; uqs on PHP built-in server integration a month ago; quocdepptry on [Guide] How to actually separate Frontend (User) and Backend (Admin) on Yii2 Advanced a month ago; Davide on Yii2 - Upgrading to Bootstrap 4 a month ago