为什么年龄过滤器不使用Laravel中的SQL查询过滤年龄?

问题描述:

我想根据年龄查询人员清单.然后,查询将使用lahir_yy列(出生年份值)计算年龄并搜索列表.

I want to query list of persons based on age. Then, the query will calculate the age using lahir_yy column(birth year value) and search for the list.

   $newitem = DB::table('itemregistrations')
                ->when(request('age'), function($query){
                    $query->whereRaw('YEAR(CURDATE()) - lahir_yy >= ?', [request('age')]);})
   ->get(); 

但是,该代码无法正常工作.它不会过滤年龄,但不会出错.

However, the code doesn't work. It doesn't filter the age but giving no error.

尝试一点零钱.关闭发送request('age').希望这会工作.

Try with small change. Send request('age') in closure. Hope this will work.

$newitem = DB::table('itemregistrations')
            ->when(request('age'), function($query){
                return $query->whereRaw('YEAR(CURDATE()) - lahir_yy >= ?', [request('age')]);})
->get();