当前位置: 首页 > 知识库问答 >
问题:

Laravel雄辩的ORM组在哪里

钱卓君
2023-03-14

如何将以下查询转换为Laravel4雄辩ORM?

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

共有1个答案

慕容渊
2023-03-14

这样地:

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

看一下文档,你可以用Elount和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();
 类似资料:
  • 在有说服力的ORM上,是否有一种较短的方法在Laravel中插入数据? 目前我正在这样做: 就像你看到的,所有的列都像属性一样命名。 我知道您可以将对象与all()函数和fetch()一起使用。但是我从另一个对象(soap服务器)获取数据。 有没有办法转换这个?也许在模型上?

  • 我正在编写php代码,对于当前的项目,我正在使用laravel框架(带有雄辩的ORM)。在这个项目中,我创建了两个表:用户和任务。我想在这些表之间创建一个“一对多”的关系。因此,一个用户可以有许多任务。在Tasks类中,我创建了方法所有者(): 但是当我以前得到用户名的时候 我得到这个异常:未定义的属性:illumb\Database\Eloquent\Relations\BelongsTo::$

  • 嗨,我正在学习laravel。我使用雄辩的ORM删除方法,但我得到了不同的结果。不是真的或假的,而是空的。我设置了一个资源路由,在UsersController中有一个销毁方法。 但是我总是得到一个响应{“status”:“0”,“msg”:“failed”},数据库中的记录被删除。 然后我使用dd($res)。它在页面中显示null。 但是从课程中我学习到它返回一个布尔值true或false。

  • 我的状态帖子有以下数据库设置。对于每一篇文章,用户可以喜欢这篇文章,评论这篇文章,甚至可以由作者在原始文章中添加标签。 我试图设置我的足智多谋的控制器后带回所有的数据通过JSON对象,但我不能正确地找到评论,喜欢或标记用户名。如果有区别的话,我会用哨兵2进行认证。 以下是数据库设置: 我的Post控制器,我只是有一个简单的页面,可以显示所有内容。我不想循环查看文件中的任何内容,我只想返回json完

  • 如何使用laravel雄辩的ORM关系在多个表中发出一个插入请求。即 表1:用户 id 姓名 电子邮件 表2:员额 id 表3:图像 id 用户id 邮政id 图像名称 关系 表id引用了其他两个表中的 表与

  • 嘿,伙计们,我是拉威尔的新朋友。我有这张桌子 我如何能够获取每个科目的is_correct列计数? 谢谢伙计们!