gpt4 book ai didi

php - Laravel Eloquent 查询中的 Intval 函数

转载 作者:行者123 更新时间:2023-11-29 06:52:04 26 4
gpt4 key购买 nike

我正在做过滤器脚本。用户写入age_to,我得到最大年龄的用户。问题是我的用户表中只有完整的出生日期。如何获取所有数据?我认为 intval 函数很好,但我不知道如何在 mysql 查询中使用它。

$user = User::with('user_x')->whereHas('user_x', function($query) use ($request) { 
$birth_date = Carbon::now()->subYears($request->age_to)->toDateString();

return $query->where('date_of_birth', '<=', $birth_date);
})->get();

最佳答案

Intval 可能不会做你想要的事情,但 Carbon 可能会。既然您要考虑年龄,那么请使用 Carbon 的日期 setter :

$birth_date = Carbon::now()->subYears($request->age_from)->toDateString(); // Sets the birth date to today's date minus the requested years

然后您可以在查询中使用它:

$user = User::with('user_x')->whereHas('user_x', function($query) use ($birth_date) { 
return $query->where('date_of_birth', '<=',$birth_date);
})->get();

关于php - Laravel Eloquent 查询中的 Intval 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47054874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com