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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效