【问题标题】:Laravel eloquent ORM group whereLaravel 雄辩的 ORM 群在哪里
【发布时间】:2014-04-14 20:35:40
【问题描述】:

如何将以下查询转换为 Laravel 4 eloquent ORM?

select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))

【问题讨论】:

    标签: laravel-4 eloquent


    【解决方案1】:

    像这样:

    <?php
    
    $results = DB::table('table')
                 ->where(function($query) use ($starttime,$endtime){
                     $query->where('starttime', '<=', $starttime);
                     $query->where('endtime', '>=', $endtime);
                 })
                 ->orWhere(function($query) use ($otherStarttime,$otherEndtime){
                     $query->where('starttime', '<=', $otherStarttime);
                     $query->where('endtime', '>=', $otherEndtime);
                 })
                 ->orWhere(function($query) use ($anotherStarttime,$anotherEndtime){
                     $query->where('starttime', '>=', $anotherStarttime);
                     $query->where('endtime', '<=', $anotherEndtime);
                 })
                 ->get();
    

    查看documentation,了解您可以使用 Eloquent 和Query Builder 做的更酷的事情。

    //编辑: 甚至将整个 where 子句包裹在大括号中(就像在您的问题中一样),您可以这样做:

    <?php
    
    $results = DB::table('table')
                 //this wraps the whole statement in ()
                 ->where(function($query) use ($starttime,$endtime, $otherStarttime,$otherEndtime, $anotherStarttime,$anotherEndtime){
    
                     $query->where(function($query) use ($starttime,$endtime){
                         $query->where('starttime', '<=', $starttime);
                         $query->where('endtime', '>=', $endtime);
                     });
    
                     $query->orWhere(function($query) use ($otherStarttime,$otherEndtime){
                         $query->where('starttime', '<=', $otherStarttime);
                         $query->where('endtime', '>=', $otherEndtime);
                     });
    
                     $query->orWhere(function($query) use ($anotherStarttime,$anotherEndtime){
                         $query->where('starttime', '>=', $anotherStarttime);
                         $query->where('endtime', '<=', $anotherEndtime);
                     });
                 })
                 ->get();
    

    【讨论】:

    • 但是当我们使用闭包时,框架会处理 $query。我们如何处理 $startTime 和 $endTime。另一方面,如果我传递诸如 function($query,$startTime) {} 之类的参数,它会引发错误 Undefined variable: dateRange 它为 Model::{closure}() 提供 Missing argument 2
    • @BishwarupDas 您可以像这样将外部变量传递到闭包的范围内:$query-&gt;where(function($query) use ($starttime, $endtime) { ... }); 以下是一些示例:php.net/manual/en/functions.anonymous.php(特别是示例 #3)
    • select d1.update_id from (select update_id, count(update_id) as ct from updates_tags where tag_id in (67,33,86,55) group by update_id) as d1 where d1.ct=4 How将其转换为 laravel 查询...?
    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多