繁体   English   中英

Laravel 查询过滤掉事务并检查两个日期之间

[英]Laravel Query Filtering Out Transactions and Checking Between Two Dates

我的查询正在获取两个日期之间的现有交易。 但是, whereNotIn 函数不会过滤掉交易。 它仍然选择被取消、拒绝或完成的交易。 我的查询似乎有什么问题? 谢谢

$associates = Associate::join('transactions', 'associate.associate_id', '=', 'transactions.associate_id')
            ->select('associate.associate_id')
            ->whereNotIn('status', ['Cancelled', 'Declined', 'Finished'])
            ->whereBetween('startdate',[$start_date, $end_date])
            ->orWhereBetween('enddate',[$start_date, $end_date])
            ->orWhere(function ($query) use ($request) {
                $start_date = new DateTime($request->input('start_date'));
                $end_date = new DateTime($request->input('end_date'));
                $query->where('startdate','>=',$start_date)
                  ->where('enddate','<=',$end_date);
            })
            ->get();

你应该组织你的'wheres'来做你想做的事情,要做到这一点,你可以使用 where 和 close :

  $associates = Associate::join('transactions', 'associate.associate_id', '=', 'transactions.associate_id')
            ->select('associate.associate_id')
            ->whereNotIn('status', ['Cancelled', 'Declined', 'Finished'])
            ->where(function ($query) use ($request, $end_date, $start_date) {
                $query->whereBetween('startdate',[$start_date, $end_date])
                    ->orWhereBetween('enddate',[$start_date, $end_date])
                    ->orWhere(function ($query) use ($request) {
                        $start_date = new DateTime($request->input('start_date'));
                        $end_date = new DateTime($request->input('end_date'));
                        $query->where('startdate','>=',$start_date)
                            ->where('enddate','<=',$end_date);
                    });
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM