Undefined variable: searchModel

I have faced an error

"Undefined variable: searchModel"

I am using Kartik Gridview Extension.

My Controller Code SiteController.php :




public function actionGridviewkartik() {

   $dataProvider = new ActiveDataProvider([

      'query' => AddpostForm::find(),

      'pagination' => [

          'pageSize' => 20,

      ],

   ]);

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

      'dataProvider' => $dataProvider

   ]);

}



My View Code in gridviewkartik.php :




<?php

use kartik\grid\GridView;

echo GridView::widget([

    'dataProvider'=> $dataProvider,

    'filterModel' => $searchModel,

    'columns' => $gridColumns,

    'responsive'=>true,

    'hover'=>true

]);

?>



My Models Code in AddpostForm.php :




<?php

namespace frontend\models;

use yii\db\ActiveRecord;


class AddpostForm extends ActiveRecord

{

    public static function tableName(){

        return 'company';

    }


    public function rules(){

        return [

            [ 'company_name', 'trim' ],

            [ 'company_description', 'trim' ],

        ];

    }

}



Please Help Me.

Error Screenshot:

7546

Undefined_Variable_SearchModel.png

you need to pass an instance of your model to the view


public function actionGridviewkartik() {

   $addPostForm = new AddpostForm();

   $dataProvider = new ActiveDataProvider([

      'query' => AddpostForm::find(),

      'pagination' => [

          'pageSize' => 20,

      ],

   ]);

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

      'searchModel' => $addPostForm,

      'dataProvider' => $dataProvider

   ]);

}

Yes, Its Works. Thanks.

The Error gone. But I faced new error.

Undefined variable: gridColumns

7547

Undefined_Variable_gridColumns.png

So what you think about this error?

As the error message clearly states, a variable named "gridColumns" is not defined in your code.

Usually this error message is enough for a developer to correct the error.

You have to have a basic skill of PHP language to develop a web app in Yii framework (or any other PHP frameworks in general).

Of course we could help you in this particular case, but we strongly recommend you learn PHP step by step before you proceed.

I would agree with @softark, please go through a php book or take one of those online course and get yourself acquainted with basic php/programming principals before you jump into a framework.