Laravel雄辩的ORM集团在哪里

Bis*_*Das 32 eloquent laravel-4

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

select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
Run Code Online (Sandbox Code Playgroud)

Qua*_*unk 73

像这样:

<?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();
Run Code Online (Sandbox Code Playgroud)

查看文档,了解使用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();
Run Code Online (Sandbox Code Playgroud)

  • @BishwarupDas你可以将外部变量传递到Closure的范围,如下所示:`$ query-> where(function($ query)use($ starttime,$ endtime){...});`这里有一些示例:http ://www.php.net/manual/en/functions.anonymous.php(特别是示例#3) (2认同)