即使年份不正确也会返回记录

问题描述:

我正在尝试做一些过滤,只在指定日期获取这些记录,下面是我的完整雄辩查询。即使年份不正确也会返回记录

$products = product::whereYear('created_at', '2009') 
      ->where('type_id','!=','') 
      ->orWhereNotNull('type_id') 
      ->with('type','product_warehouses.warehouse') 
      ->get(); 

目前,我只有记录在2017年内,但它仍然给我的记录,即使我指定日期为'2009'。但是,如果我拿出这

->orWhereNotNull('type_id') 

该查询的工作原理就像它给我记录基于给定年份。任何想法,请帮助吗?

+0

的提示是** **或'WhereNotNull'因为现在你有一个OR条件。我想你想看看[这个答案](https://*.com/questions/16995102/laravel-4-eloquent-where-with-or-and-or) –

+0

是的,这也是我的想法。现在就修 –

修复,我可以只使用代替,

$products = product::whereYear('created_at', date('Y'))->where(function($query){ 
     $query->where('type_id','!=',''); 
     $query->orWhere('type_id','!=',null); 
    })->with('type','product_warehouses.warehouse')->get(); 

->where('type_id','!=','') 
->orWhere('type_id','!=',null)