php - Laravel Eloquent 查询中的 Intval 函数

标签 php mysql laravel

我正在做过滤器脚本。用户写入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/

相关文章:

php - Laravel 5 - 重定向到 HTTPS

php - 在 Ratchet 的 MessageComponentInterface 中访问 Eloquent ORM

php - 为什么 trim() 会使一个不存在的变量存在?

java - 如何?使用 Retrofit2 将当前日期从 Android 插入到 mySQL 数据库中

php - 如何在 php 上从 plsql 打开自定义类型?

php - jQGrid - 从 Grid1 中选择行以填充 Grid2(MySQL、jQuery、PHP)

php - fatal error : Uncaught exception 'Zend_Exception' with message 'No entry is registered for key ' Zend_Db''

mysql - 如果 MySQL 中的某行大于另一个值,则将其添加到该行

php - 在 PHP 中调用(循环)MySQL 行列表会产生意外结果

laravel - 在 Ubuntu 上使用 Sail 和 PhpStorm 在 Laravel 8 上运行 Xdebug