【问题标题】:Filter record through dates not working laravel通过日期过滤记录不起作用 laravel
【发布时间】:2021-10-18 11:54:15
【问题描述】:

我的表中有一个 expiry_date (type=date) 列

$currentDate = date('Y-m-d');
$Data = Post::whereDate('expiry_date','<=',$currentDate)->where(['status' => 'active'])->orWhere(['p_id' => 3])->select('id','title','status','p_id','expiry_date')->orderBy('id', 'DESC')->get();

如果当前日期大于到期日期,我想过滤数据,则不应显示这些记录,但在我的场景中,我仍在获取记录。

任何解决方案谢谢。

【问题讨论】:

    标签: laravel laravel-5 eloquent laravel-4


    【解决方案1】:

    您必须将orWhere 子句组合在闭包中。闭包分组就像实际查询中的()

    $Data = Post::whereDate('expiry_date','<=',$currentDate)
        ->where(function($query){
            return $query
                ->where(['status' => 'active'])
                ->orWhere(['p_id' => 3]);
        })
        ->select('id','title','status','p_id','expiry_date')
        ->orderBy('id', 'DESC')
        ->get();
    
    

    但是,因为我不知道你的项目 - 我可能对分组有误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2020-08-17
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2013-11-08
      相关资源
      最近更新 更多