Filter Yii2 GridView by url parameter

I’m linking related models together via a link. ONE registration has MANY players.

registration.ID = players.registrationID

my link goes to players/index&registrationID=29

Gridview shows up, but none of the results are filtering to only show players with a registrationID of 29.


public function actionIndex()


        $searchModel = new PlayersSearch();

        if (isset($_GET['Players']))

            $searchModel->attributes = $_GET['Players'];

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,



actionList (also tried linking to players/list&registrationID=29):

public function actionList()


        $model = new PlayersSearch();

        $model->unsetAttributes(); // clear default vals

        if (isset($_GET['Players'])) {

            $model->attributes = $_GET['Players'];


        $criteria = new CDbCriteria;

        $criteria->order = 'ID ASC';

        //regardless of the filter always apply this criteria

        $model->CDbCriteria = $criteria;

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

            'model' => $model]);


players/index.php Gridview widget:

<?php Pjax::begin(); ?>    <?= GridView::widget([

        'model' => $model,

        'filterModel' => $model,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],








            // 'street',

            // 'zip',

            ['class' => 'yii\grid\ActionColumn'],


    ]); ?>

<?php Pjax::end(); ?>

PlayersSearch model:

public function search($params)


        $query = players::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([

            'query' => $query,


        // setup sorting attributes BEFoRE $this->load($params)


            'attributes' =>[



                'fullName' => [

                    'asc' => [

                        'first' => SORT_ASC, 

                        'last' => SORT_ASC],

                    'desc' => [

                        'first' => SORT_DESC,

                        'last' => SORT_DESC],

                    'label' => 'Full Name',

                    'default' => SORT_ASC








        if (isset($_GET['PlayersSearch']) && !($this->load($params) && $this->validate())) {

            // uncomment the following line if you do not want to return any records when validation fails

            // $query->where('0=1');


            return $dataProvider;



        // grid filtering conditions

        $query->andFilterWhere(['ID' => $this->ID]);

        $query->andFilterWhere(['registrationID' => $this->registrationID]);

        $query->andFilterWhere(['first' => $this->first]);

        $query->andFilterWhere(['last' => $this->last]);

        $query->andFilterWhere(['email' => $this->email]);

        $query->andFilterWhere(['phone' => $this->phone]);

        $query->andFilterWhere(['city' => $this->city]);

        $query->andFilterWhere(['state' => $this->state]);

        $query->andWhere('first LIKE "%' . $this->fullName . '%" ' . 'OR last LIKE "%' . $this->fullName . '%"');

        return $dataProvider;


PLEASE help me figure out what I’m doing wrong. I could do it in Yii 1, but don’t know how in Yii2.

The parameters need to be separated from the url with a "?", not a "&". You use "&" to link multiple parameters.

Also your parameter should probably be named "PlayersSearch[registrationID]". Try this url:


This part of your code doesn’t really make sense:

if (isset($_GET['Players']))

    $searchModel->attributes = $_GET['Players'];

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

First you are filling the searchModel attributes manually, and then you are telling searchModel to search by what it can find in "Yii::$app->request->queryParams".

Thank you Patrick!!! I am linking to the players page with a function, and that is passing the & before the parameter, however I changed it to include the PlayersSearch[] as you suggested and it works!!!:

public function getPlayersEditLink()


        $url = Url::to(['players/index', 'PlayersSearch[registrationID]'=>$this->ID]);

        $options = [];

        return Html::a('Edit Players',$url,$options);


I used the part you said doesn’t make sense when I felt like I had exhausted all other options. It was something I was trying out of desperation. That part will be removed.