如何执行条件查询

时间:2018-12-05 03:57:51

标签: php laravel

我有一个3列的表格。我想根据是否选中了相应的复选框来执行查询。我没有实际期望匹配结果的任何结果。我该如何实现?

这是我尝试失败的结果:

$products = OtherProduct::orderBy('id', 'DESC');
if($request->col1) {
  $products->where('col1', 'value to search');
}
if($request->col2) {
  $products->where('col2', 'value to search');
}
if($request->col3) {
  $products->where('col3', 'value to search');
}
$products->get();

2 个答案:

答案 0 :(得分:4)

->when(..)用于条件:

Comparable<T>

答案 1 :(得分:0)

使用Laravel class MonthYearPicker extends StatefulWidget { @override _MonthYearPickerState createState() => _MonthYearPickerState(); } class _MonthYearPickerState extends State<MonthYearPicker> { List<DropdownMenuItem> monthList = const [ DropdownMenuItem(child: Text('January'), value: 1), DropdownMenuItem(child: Text('February'), value: 2) ]; final List<DropdownMenuItem> yearList = [ DropdownMenuItem(child: Text((DateTime.now().year - 2).toString()), value: -2), DropdownMenuItem(child: Text((DateTime.now().year).toString()), value: 0), ]; int _selectedMonth; int _selectedYear; @override Widget build(BuildContext context) { return Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ DropdownButton( items: monthList, value: _selectedMonth, hint: Text('---Month---'), onChanged: (value) { _selectedMonth = value; setState(() {}); }, ), DropdownButton( items: yearList, value: _selectedYear, hint: Text('---Year---'), onChanged: (value) { _selectedYear = value; setState(() {}); }, ), ], ); } } ,然后您的代码应类似于

        body: Column(
          children: <Widget>[
        MonthYearPicker(),
            Center(
          child: RaisedButton(
                child: Text('Get Date'),
            onPressed: () {
              //How to access selected month and year from MonthYearPicker?
                },
          ),
        )
          ],