Laravel Eloquent ORM 分组查询和条件查询

53

我该如何将以下查询语句转换为Laravel 4的Eloquent ORM?

select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
2个回答

139

就像这样:

<?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();

查看文档,了解使用Eloquent和查询构建器的更多酷炫操作。

//编辑:要将整个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) {},它会给出Model::{closure}()的缺少第二个参数的错误提示。同时,如果不这样做,会抛出未定义变量:dateRange的错误提示。 - Bishwarup Das
2
@BishwarupDas 您可以通过以下方式将外部变量传递到闭包的作用域中:$query->where(function($query) use ($starttime, $endtime) { ... }); 这里有一些示例:http://www.php.net/manual/en/functions.anonymous.php(特别是示例#3) - Quasdunk
选择d1.update_id from ( 选择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如何将此转换为Laravel查询...? - Ankit

1

$jkw = DB::table('table') //这将整个语句包裹在()中 ->where(function($query) use ($starttime,$endtime, $otherStarttime,$otherEndtime, $anotherStarttime,$anotherEndtime){

             $query->where(function($query) use ($starttime,$endtime){
                 $query->where('starttime', '<=', $starttime)
                 ->where('endtime', '>=', $endtime);
             });

             $query->orWhere(function($query) use ($otherStarttime,$otherEndtime){
                 $query->where('starttime', '<=', $otherStarttime)
                 ->where('endtime', '>=', $otherEndtime);
             });

             $query->orWhere(function($query) use ($anotherStarttime,$anotherEndtime){
                 $query->where('starttime', '>=', $anotherStarttime)
                 ->where('endtime', '<=', $anotherEndtime);
             });
         })
         ->get();

轻松愉快 XD


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接