繁体   English   中英

流畅查询以下情况

[英]fluent query for the following scenario

我在数据库中有一个程序列表。 每个程序都有该程序所属的organization_id ,以及一个start_dateend_date

我的问题是,如果给出两个日期,查询应该是什么样子?我必须获取所有在特定组织的日期之间开始或在日期之间结束的程序。 以下查询返回组织范围之外的所有程序(不属于组织)。

<?php

DB::table('programs')
    ->where('organization_id', $organizationidlist[0])
    ->whereBetween('start_date', array($start_date, $end_date))
    ->orWhere(function ($query) use($start_date, $end_date, $organizationidlist) {
        $query
            ->where('organization_id', $organizationidlist[0])
            ->whereBetween('end_date', array($start_date, $end_date))
        ;
    })
    ->orderBy('created_at', 'desc')
    ->get()
;

?>

为什么用雄辩的模型不这样做呢? Laravel有一个很好的数据模型系统。 您可以轻松地通过以下方式完成您想要的事情:

Organization::find($id)->projects()->where('start_date', '>', $start)->where('end_date', '<', $end)->orderBy('created_at', 'desc')->get();

http://laravel.com/docs/eloquent

暂无
暂无

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

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