dsqbh42082 2019-04-18 07:26
浏览 264
已采纳

如何在laravel中获取开始日期和结束日期之间的所有记录? [重复]

This question already has an answer here:

In my blade file there is a search bar on leave duration column, leave duration column contail startDate & endDate,

In my database i have 2 fields start date and end date, and if i search a date who lies between startDate & endDate than it will give me output,

here in my controller file

public function listOfLeave(Request $request)
{
    $from      = date('1001-01-01');
    $to        = date('9999-12-31');
    $allLeaves = null;
    if (
        !empty($request->input('name')) ||
        !empty($request->input('leaveType')) ||
        !empty($request->input('leaveDate')) ||
        !empty($request->input('appliedDate')) ||
        !empty($request->input('status'))
    ) {
        $flg = false;
        if (!empty($request->input('name'))) {
            $flg       = true;
            $allLeaves = LeaveManagement::where('username', 'LIKE', "%{$request->input('name')}%");
        }
        if (!empty($request->input('leaveType'))) {
            if ($flg) {
                $allLeaves = $allLeaves->orWhere('typeOfLeave', 'LIKE', "%{$request->input('leaveType')}%");
            } else {
                $allLeaves = LeaveManagement::where('typeOfLeave', 'LIKE', "%{$request->input('leaveType')}%");
            }
        }
        if (!empty($request->input('leaveDate'))) {
            if ($flg) {
                $allLeaves = $allLeaves->orWhereBetween('startDate', 'LIKE', "%{$request->input('leaveDate')}%");
            } else {
                $allLeaves = LeaveManagement::where('startDate', 'LIKE', "%{$request->input('leaveDate')}%");
            }
        }
        if (!empty($request->input('appliedDate'))) {
            if ($flg) {
                $allLeaves = $allLeaves->orWhere('startDate', 'LIKE', "%{$request->input('appliedDate')}%");
            } else {
                $allLeaves = LeaveManagement::where('startDate', 'LIKE', "%{$request->input('appliedDate')}%");
            }
        }
        if (!empty($request->input('status'))) {
            if ($flg) {
                $allLeaves = $allLeaves->orWhere('status', 'LIKE', "%{$request->input('status')}%");
            } else {
                $allLeaves = LeaveManagement::where('status', 'LIKE', "%{$request->input('status')}%");
            }
        }
        $allLeaves = $allLeaves->orderBy('username', 'ASC')
            ->orderBy('typeOfLeave', 'ASC')
            ->orderBy('startDate', 'ASC')
            ->orderBy('startDate', 'ASC')
            ->orderBy('status', 'ASC')
            ->paginate(2);
        return view('pages.newleaverequest')->with(['allLeaves' => $allLeaves]);
    } else {
        $allLeaves = LeaveManagement::orderBy('username', 'ASC')->paginate(5);
    }
    $allLeaves->appends(['name' => Input::get('name')]);
    return view('pages.newleaverequest', compact('allLeaves'));
}

How can i get records who lies between startdate and enddate???

</div>
  • 写回答

4条回答 默认 最新

  • duanhai1455 2019-04-18 08:08
    关注

    Ok. I think this should help you:

    if (!empty($request->input('leaveDate'))) {
        if ($flg) {
            $allLeaves = $allLeaves->whereDate('endDate', '<', $request->input('leaveDate'));
        } else {
            $allLeaves = LeaveManagement::whereDate('endDate', '<', $request->input('leaveDate'));
        }
    }
    
    if (!empty($request->input('appliedDate'))) {
        if ($flg) {
            $allLeaves = $allLeaves->whereDate('startDate', '>', $request->input('appliedDate'));
        } else {
            $allLeaves = LeaveManagement::whereDate('startDate', '>', $request->input('appliedDate'));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办