php - Laravel `Item` 有很多,只有在过去时才返回

标签 php mysql laravel

在我的项目中,hire 可以有很多 hire_days。每个 hire_day 都有一个 startend 时间戳。 我想创建一个查询,它将只返回所有关联的 hire_day.end 时间过去的雇员。 到目前为止,我的查询:

$hires = Hires::join('hires_days', function ($join) {
            $join->on('hire_id', '=', 'hires.id');
        })
        ->select('hires.id', 'hires.account_id', 'hires.job_number', 'hires.site_id', \DB::raw('MAX(end) AS end'))
        ->orderBy('hires_days.end', 'asc')
        ->groupBy('hires.id')
        ->paginate(10);

如果他们的 hire_days 中至少有一个已经过去,这将返回 hires,而忽略了一些相关的 hire_days 仍然存在的事实 future 。

最佳答案

我认为您需要的是添加到您的查询中:

->having('end', '<', time())

关于php - Laravel `Item` 有很多,只有在过去时才返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913165/

相关文章:

php - codeigniter JOIN 返回错误的 id

php mysql 返回代码而不是文件

php - PDODriver Exception Doctrine 找不到驱动程序

php - 警告 : mysql_fetch_array(): supplied argument is not a valid MySQL result resource

mysql - 更新mysql中的多行?

如果未选中复选框,PHP 会更新数据库

php - 创建具有关系的新 Eloquent 实例

php - 正确检查 MySQLi OOP 错误

laravel - Laravel 5.6 中的 url() 与 route()

mysql - Laravel 中未提交表单?