Laravel范围的模型自定义场值

发布于 2025-01-22 20:24:28 字数 1107 浏览 0 评论 0原文

我的模型如下

class WorkingTime extends Model
{
    protected $table = 'working_times';
    protected $fillable = ['day', 'start_time', 'finish_time', 'user_id'];
    public function scopeAfterStart($query, $start)
    {
        return $query->where('start_time', '<=', $start);
    }

    public function scopeBeforeFinish($query, $finish)
    {
        return $query->where('finish_time', '>=', $finish);
    }
}

,可以使用以下查询,

WorkingTime::afterStart(1200)->beforeFinish(1800)->get();

我需要做的是,在work_times表中start_time_time&gt; finish_time值,我必须检查finish_time + 2400&gt; = $ teright。更具体地说,必须发生以下查询之类的事情,

with new_working_times as (
    select       
        start_time,
        case when start_time > finish_time then
            finish_time + 2400 
        else
            finish_time
        end as finish_time   
)
select * from new_working_times
where finish_time > {finishTime} and start_time < {start_time}

我该如何通过雄辩和范围进行操作?

I have a model as follows

class WorkingTime extends Model
{
    protected $table = 'working_times';
    protected $fillable = ['day', 'start_time', 'finish_time', 'user_id'];
    public function scopeAfterStart($query, $start)
    {
        return $query->where('start_time', '<=', $start);
    }

    public function scopeBeforeFinish($query, $finish)
    {
        return $query->where('finish_time', '>=', $finish);
    }
}

and I can use the following query

WorkingTime::afterStart(1200)->beforeFinish(1800)->get();

what I need to do is, in working_times table when the start_time > finish_time value, I have to check if finish_time + 2400 >= $finish. To be more specific, something like the following query has to be happened

with new_working_times as (
    select       
        start_time,
        case when start_time > finish_time then
            finish_time + 2400 
        else
            finish_time
        end as finish_time   
)
select * from new_working_times
where finish_time > {finishTime} and start_time < {start_time}

How can I do this via eloquent and scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文