Why age filter doesn't filter the age using sq

2019-08-28 07:33发布

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.

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-28 08:08

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(); 
查看更多
登录 后发表回答