Php Laravel:比较数据库中两列之间的日期不起作用

Php Laravel:比较数据库中两列之间的日期不起作用,php,sql,laravel,date,Php,Sql,Laravel,Date,我们正在尝试用我们的项目来构建和使用api。我的问题是,当我需要获取一个特定的事件时,它有一个列date\u from和date\u to,它总是获取数据,即使事件已经完成。我想知道这两个日期剩下的时间里发生的具体事件。我的问题是,当date\u from和date\u to等于或大于今天的日期时,它会显示出来,而在日期完成后它不会显示出来。这是我的一些代码 索引 公共功能索引(请求$Request) { $person=User::find(Auth::User()->id); $now=碳:

我们正在尝试用我们的项目来构建和使用api。我的问题是,当我需要获取一个特定的事件时,它有一个列
date\u from
date\u to
,它总是获取数据,即使事件已经完成。我想知道这两个日期剩下的时间里发生的具体事件。我的问题是,当
date\u from
date\u to
等于或大于今天的日期时,它会显示出来,而在日期完成后它不会显示出来。这是我的一些代码

索引

公共功能索引(请求$Request)
{
$person=User::find(Auth::User()->id);
$now=碳::now()->toDateString();
如果(空($人))
返回响应()->json([
“代码”=>“03”,
“状态”=>“无效用户”
], 403);
$role=$person->role;
如果($role==2 | |$role==3){
$level=Event::active()->with(['groups'])
->其中('school\u id','=',$person->school\u id)
->其中('group_id','=',$role)
->orWhere('group_id','=',$person)
->其中('status','=',1)
->其中('active','=',1)
->whereRaw(“date_from=date(“$now”))
->get();
返回响应()->json($level);
}
结果是邮递员

{
        "id": 1,
        "name": "Final",
        "content": "<p>Sample</p>",
        "group_id": 2,
        "event_date": "2019-09-01", 
        "date_from": "2019-09-20", //sept 20, 2019. The event started
        "date_to": "2019-10-03",//oct, 3 2019. The event end date. still gets the data even the date was not today.
        "time_from": "16:15:00",
        "time_to": "22:15:00",
        "event_place": "Room",
        "image": "",
        "school_id": 1,
        "smsnotify": 0,
        "appnotify": 1,
        "status": 1,
        "active": 1,
        "updated_by": null,
        "created_at": "2019-09-27 08:29:13",
        "updated_at": "2019-10-04 06:18:43",
        "users_id": 2,
        "groups": {
            "id": 2,
            "name": "Teachers",
            "description": "Teachers",
            "updated_by": null,
            "created_at": null,
            "updated_at": null
        }
    }
{
“id”:1,
“名称”:“最终”,
“内容”:“样本

”, “组id”:2, “活动日期”:“2019-09-01”, “日期自”:“2019-09-20”//2019年9月20日。活动开始 “date_to”:“2019-10-03”,//2019年10月3日。事件结束日期。即使日期不是今天,仍会获取数据。 “时间从”:“16:15:00”, “时间”:“22:15:00”, “活动地点”:“房间”, “图像”:“, “学校id”:1, “smsnotify”:0, “appnotify”:1, “地位”:1, “主动”:1, “更新者”:空, “创建时间”:“2019-09-27 08:29:13”, “更新时间”:“2019-10-04 06:18:43”, “用户id”:2, “团体”:{ “id”:2, “姓名”:“教师”, “说明”:“教师”, “更新者”:空, “created_at”:空, “更新位置”:空 } }
试试这个

$level = Event::active()->with(['groups'])
            ->where([['school_id', '=', $person->school_id],['group_id', '=', $role],['status', '=', 1],['active', '=', 1]])
            ->orWhere('group_id', '=', $person)
            ->whereRaw("CURDATE() between date_from and date_to")
            ->get();

变量
$now
,正如您所定义的,应该已经是当前时间戳的仅日期组件。因此没有理由不使用常规
where()

请仔细注意括号。但是,正如您所写的,由于操作顺序的原因,它可能会出现不符合您预期的结果。

试试看

$level = Event::active()->with(['groups'])
  ->where('school_id', '=', $person->school_id)
  ->whereIn('group_id', [$person,$role])
  ->where('status', '=', 1)
  ->where('active', '=', 1)
  ->where('date_from', '<=', $now)
  ->where('date_to', '>=', $now->addDays(1))
  ->get();
$level=Event::active()->with(['groups'])
->其中('school\u id','=',$person->school\u id)
->其中('group_id',[$person,$role])
->其中('status','=',1)
->其中('active','=',1)
->其中('date_from','=',$now->addDays(1))
->get();

您的碳日期应与数据库服务器中设置的格式相匹配

$now = Carbon::now()->toDateString()->format('Database Format');
另外,您不需要在date函数中传递日期变量

 ->where('date_from', '<', $now)

->where('date\u from','
->whereRaw('date\u to>=date('$now.'))
您可以使用
where('date_from','仍然在邮递员中获取相同的数据,先生。@zainfarooq不起作用,先生。仍然在获取数据@jitheshjose仍然在邮递员中获取数据。但是当我进行其他比较时,例如
status=0
,它会起作用。只有日期不起作用,先生。我发现了一些东西。如果我注释掉
->或者注释掉在哪里('group_id'、'='、$person)
它会起作用。但是我应该怎么做才能不删除
或where
?@RonaldBistillo我也学过这个。你应该在这里使用
where
。哦,
或where
子句是问题所在,先生?使用
或where
where
有什么问题吗?仍然从邮递员那里获取数据。B但是,当我进行其他比较时,如status=0,它会起作用。只有日期不起作用。我发现了一些东西,先生。如果我注释掉
->或where('group\u id','=',$person)
它会起作用。但是为了不删除
或WHERE
,我应该做什么?编辑后的答案请这样尝试。仍然是相同的sir注意,上面的
WHERE
逻辑只会匹配“开始”和“截止”发生在今天午夜的记录。因此我必须添加
$now->addDays(1)
以便正常工作。对吗?谢谢,先生。问题出在
或where
子句中的我的代码上,这就是为什么即使日期比较正确,它也会不断获取数据。请参阅@Tim Biegeleisen答案
$level = Event::active()->with(['groups'])
  ->where('school_id', '=', $person->school_id)
  ->whereIn('group_id', [$person,$role])
  ->where('status', '=', 1)
  ->where('active', '=', 1)
  ->where('date_from', '<=', $now)
  ->where('date_to', '>=', $now->addDays(1))
  ->get();
$now = Carbon::now()->toDateString()->format('Database Format');
 ->where('date_from', '<', $now)