Answer the question
In order to leave comments, you need to log in
How to solve yii2 GridView search problems?
Good afternoon! I created an admin panel with a gridview widget via crud. And I changed the display in the index file
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['class' => 'yii\grid\ActionColumn'],
// 'id',
'name',
'price',
//'category_id',
[
'attribute'=>'category_id',
'format'=>'raw',
'value'=>function($data){
$catr = Category::find()->where(['id'=>$data->category_id])->one();
return $catr->name;
}
],
],
]); ?>
Answer the question
In order to leave comments, you need to log in
You need to change the SearchModel: add the category_name property there and change the database query so that on the one hand this property is correctly selected (add to select and do not forget about JOIN), and on the other hand, it is searched for (if set, then add the corresponding andFilterWhere ).
Then instead of category_id in GridView it will be possible to use category_name.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question