简体   繁体   English

Laravel`Item` 有很多,只有当所有都在过去时才返回

[英]Laravel `Item` has many, return only if ALL are in the past

In my project, a hire can have many hire_days .在我的项目中,一个hire可以有很多hire_days Each hire_day is has a start and end timestamp.每个hire_day都有一个startend时间戳。 I want to create a query where it will return only the hires where ALL of it's associated hire_day.end times are in the past.我想创建一个查询,它只返回所有关联的hire_day.end时间都在过去的员工。 So far, my query:到目前为止,我的查询:

$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);

This will return hires if at least one of their hire_days is in the past, ignoring the fact that there are some associated hire_days still in the future.如果他们的hire_days中至少有一个是过去的,这将返回hires ,忽略未来仍有一些相关的hire_days这一事实。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM