Friends, there is such a widget created in the course of the video tutorial. Maybe I missed something, but for the second day I cannot find the cause of the error, which is that the added filter does not want to be displayed as index.php generated by CRUD. Those. input is not displayed.

<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', [ 'attribute' => 'category_id', 'filter' => Category::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => function(Product $product){ return $product->category->name; }, ], [ 'attribute'=>'name', 'filter' => Product::find()->select(['name','id'])->indexBy('id')->column(), 'value' => function(Product $product){ return $product->name; } ], 'content:ntext', 'price', [ 'attribute' => 'Tags', 'filter' => Tag::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => function(Product $product) { return implode(', ', ArrayHelper::map($product->tags, 'id', 'name')); }, ], [ 'attribute' => 'active', 'format' => 'boolean', 'filter' => [0 => 'Нет', 1=> 'Да'], ], ['class' => 'yii\grid\ActionColumn'], ], ]); ?> 
  • Filter by Tags interests ... this field is not native to the widget and it is for it that filter is not displayed - AndreyOv

2 answers 2

From the code it is clear that the model has multiple links with Tags (hasMany). To be able to search for them, you need to consider 2 points:

1) the model has no explicit attribute tags (fields in the database), there is only getTags() getter, therefore the searchModel does not know what we want from it until we add a public property to it that stores the filter field entered text:

 public $tags; // сюда будет попадать текст, введённый в input фильтра 

also do not forget to add it to the rules for validating the input data.

2) in order for the search to actually work, in the search model you need to supplement the query in the database as follows:

 $query = Product::find()->joinWith('tags'); $query->andFilterWhere(['LIKE', Tags::tableName() . '.name', $this->tags]); 

    Often there was a similar problem, basically the error was in the search model that I transmitted in the GridView, (for example, in your case of the absence of the Tags attribute announcement) in the $ searchModel, in such cases this post helps me a lot. Although I am inclined to assume that an error in a capital letter in the attribute name, try tags instead of Tags in the end you should be able to: [ 'attribute' => 'tags', 'filter' => Tag::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => function(Product $product) { return implode(', ', ArrayHelper::map($product->tags, 'id', 'name')); }, ], [ 'attribute' => 'tags', 'filter' => Tag::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => function(Product $product) { return implode(', ', ArrayHelper::map($product->tags, 'id', 'name')); }, ],