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

时间:2019-04-18 07:26:39

标签: php laravel date

在我的刀片文件中,休假持续时间列上有一个搜索栏,休假持续时间列包含startDate和endDate,

在我的数据库中,我有2个字段开始日期和结束日期,如果我搜索介于startDate和endDate之间的日期,它将为我提供输出,

在我的控制器文件中

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'));
}

如何获取起始日期和结束日期之间的记录?

4 个答案:

答案 0 :(得分:1)

好的。我认为这应该对您有帮助:

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'));
    }
}

答案 1 :(得分:0)

您可以使用..-> whereBetween('your_column_name_date',[$ from,$ to]);

答案 2 :(得分:0)

在您的查询中,您将需要一个where子句来过滤这些结果。要获取特定范围内的数据,可以使用whereBetween子句。

语法如下:

    $allLeaves = $allLeaves->where('startDate', '>', $from)
                            ->where('endDate', '<', $to)
                            ->orderBy('username', 'ASC')
                            ->orderBy('typeOfLeave', 'ASC')
                            ->orderBy('startDate', 'ASC')
                            ->orderBy('startDate', 'ASC')
                            ->orderBy('status', 'ASC')
                            ->paginate(2);

答案 3 :(得分:0)

这里是从users表中获取记录,并获取记录之间给定的$ start和$ end日期的所有数据。因此,您可以看到以下简单示例并了解其工作原理。

$start = '2019-04-01';
$end = '2019-04-18';
$users = User::select("users.*")

                ->whereBetween('created', [$start, $end])

                ->get();


dd($users);