I need to search a range for a field. How do I apply a between or greater than/less than statement to the search model.
Something similar to this. However those attributes aren't valid in the Search Model
$params['MlsSearch']['min_price'] = 10;
$params['MlsSearch']['max_price'] = 100;
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search($params);
1) Define attributes in the model.
class ModelSearch extends Model
{
public $min_price;
public $max_price;
/*....*/
}
2) Make attributes safe
public function rules()
{
return [
/*... */
[['min_price', 'max_price', ], 'safe'],
]
}
3) Modify Search Function
public function search($params)
{
/*... */
$query->andFilterWhere(['>', 'price', $this->min_price]);
$query->andFilterWhere(['<', 'price', $this->max_price]);
/*... */
}
You can also use BETWEEN construction:
$query->andFilterWhere(['between', 'price', $this->min_price, $this->max_price]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With