如何获取两个日期范围之间的数据?

时间:2019-04-15 06:03:34

标签: php laravel eloquent

我想从两个不同的日期范围中获取数据。我如何才能做到这一点?我尝试过并获取了开始日期的数据。

控制器

public function detail(Request $request,$id)
{
    if($request->startdate) {
        $advance =Advancepayment::where('date',$request->startdate)->get();
    } else {
        $advance = Advancepayment::all();
    }

    return view('admin.managesalary.detail',compact('advance'));
}

刀片文件

<form action="{{route('managesalary.detail',$user->id)}}" 
      method="GET" 
      class="form-horizontal"
>
    <div class="card-body">
        <h4 class="card-title">Search</h4>
        <div class="form-group">
            <!-- Date Picker -->
            <div class="input-group date " id="startDate">
                <strong>From</strong>
                <input 
                    type='date'
                    value="{{request()->startdate}}"
                    name="startdate"
                    class="form-control"
                />
            </div>
            <!-- Time Picker -->
            <div class="input-group date" id="startTime">
                <strong>To</strong>
                <input type='date'
                    value="{{request()->enddate}}"
                    name="enddate"
                    class="form-control"
                />
            </div>
        </div>
    </div>
    <br><br>
    <div class="border-top">
        <div class="card-body">
            <button type="submit" class="btn btn-success">Search</button>
            <a href="{{route('managesalary.detail',$user->id)}}"
               class="btn btn-md btn-danger"
            >
                Clear
            </a>
        </div>
    </div>
</form>

路线

Route::get('managesalary/detail/{id}',[
    'as' => 'managesalary.detail',
    'uses' => 'ManagesalaryController@detail'
]);

预期结果如下: enter image description here enter image description here

3 个答案:

答案 0 :(得分:3)

让我们将日期范围从去年15/04到今年15/04,代码将类似于

$from = date('2018-04-15');
$to = date('2019-04-15');

$advance = Advancepayment::whereBetween('date', [$from, $to])->get();

如果有条件,$ to为空,则可以添加条件

public function detail(Request $request,$id)
{
    $from = $request->input('startdate');
    $to = $request->input('enddate');
    if ( empty($to) && empty($from) ) {
        $advance = Advancepayment::all();
    } elseif ( empty($to) && ! empty($from) ) {
        $advance = Advancepayment::where('date', $from)->get();
        // or Advancepayment::where('date', '>', $from)->get(); depending upon your requirmeent
    } else {
        $advance = Advancepayment::whereBetween('date', [$from, $to])->get();
    }
}

答案 1 :(得分:0)

对不起,我迟到了几天之前也遇到了同样的情况

我已经创建了模型作用域

有关范围的更多信息

https://laravel.com/docs/5.8/eloquent#query-scopes

https://medium.com/@janaksan_/using-scope-with-laravel-7c80dd6a2c3d

/**
     * Scope a query to only include the last n days records
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeWhereDateBetween($query,$fieldName,$fromDate,$todate)
    {
        return $query->whereDate($fieldName,'>=',$fromDate)->whereDate($fieldName,'<=',$todate);
    }

在控制器中

如果您正在使用碳库来处理日期

将Carbon库添加到顶部

  

使用Carbon \ Carbon;

OR

  

使用Illuminate \ Support \ Carbon;

从现在起获取最近10天记录

 $lastTenDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(10)->toDateString(),(new Carbon)->now()->toDateString() )->get();

从现在起获取最近30天记录

 $lastThirtyDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(30)->toDateString(),(new Carbon)->now()->toDateString() )->get();

适合您的情况

$recordResults = ModelName::whereDateBetween('filedName','2019-04-10','2019-04-12')->get();

答案 2 :(得分:0)

public function detail(Request $request,$id)
{
$startdate=$request->input('from');
$enddate=$request->input('to');   
if(!empty($request->startdate) && !empty($enddate)) {
    $advance =Advancepayment::whereBetween('date',[$startdate,$enddate])->get();
} 
else if(!empty($request->startdate) && empty($enddate))
{
    $advance =Advancepayment::where('date','>=',$request->startdate)->get();

}
else
{
    $advance = Advancepayment::all();
}

return view('admin.managesalary.detail',compact('advance'));
}