为什么年龄过滤器不使用laravel中的sql查询过滤年龄? [英] Why age filter doesn't filter the age using sql query in laravel?

查看:134
本文介绍了为什么年龄过滤器不使用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(); 

这篇关于为什么年龄过滤器不使用laravel中的sql查询过滤年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆