Skip to main content

Filter Grid view en Model Search

In Search Model for filter

add public property
public $locatie;
add to safe in rules
 public function rules() {
    return [
        ....
        [['locatie'], 'safe'],
    ];
}
add in join
// locatie is attribute from student
$query = Gesprek::find()
            ->joinwith(['student'])

note that student is relation

public function getStudent()
{
   return $this->hasOne(Student::className(), ['id' => 'studentid']);
}
add in andFilterWhere
$query->andFilterWhere(['like', 'student.locatie', $this->locatie]);

In Search Model for sort

add sort attributes
$dataProvider->sort->attributes['locatie'] = [
     'asc' => ['student.locatie' => SORT_ASC],
     'desc' => ['student.locatie' => SORT_DESC],
];

In view

add column
[
  'attribute' => 'locatie',
  'contentOptions' => ['style' => 'width:20px;'],
  'format' => 'raw',
  'value' => 'student.locatie',
],