How to get data between two date range?

I want to get the data from two different date range.How can i achive this?I have tried and got the data for starting date.

Controller

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

blade file

<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

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

Expected result as below: enter image description here enter image description here

3 answers

  • answered 2019-04-15 06:06 Prafulla Kumar Sahu

    Let's the date range is from last year 15/04 to this year 15/04, the code will be like

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

    if there is condition the $to is empty you can put a conditional

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

  • answered 2019-04-15 06:29 Manojkiran.A

    Sorry I am late I had the Same Situation Before Few Days

    And I have created the model scope

    More About Scopes

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

    And in the Controller

    IF YOU ARE USING CARBON LIBRARY FOR HANDLING DATES

    Add the Carbon Library to top

    use Carbon\Carbon;

    OR

    use Illuminate\Support\Carbon;

    To get the last 10 days record from now

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

    To get the last 30 days record from now

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

    So For Your Situation

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

  • answered 2019-04-15 06:31 jithesh jose

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