Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rubol's avatar
Level 2

Search with date range in laravel

I have a db table dateprices

id|tour_id|start|end|price

I want to get all the price of the tour starting between the dates i.e. 2017-06-21 and 2017-06-22

I have written following query but it is giving me all rows of the table:

    public function datePrice(Request $request){
    $start = $request->start;
    $end = $request->end;

    $dates = DatePrice::where('tour_id','=',$request->product_id)
    ->whereBetween('start', array($request->start, $request->end))
    ->get();

    return response()->json($dates);
    }
0 likes
3 replies
Snapey's avatar

have you checked the formats of $request->start and end ?

rubol's avatar
Level 2

The date format of start and end is in yyyy-mm-dd:

    $(function(){$("#start").datepicker({
            autoclose:!0,
            format:"yyyy-mm-dd",
            startDate:new Date
        }),
        $("#end").datepicker({
            autoclose:!0,
            format:"yyyy-mm-dd",
            startDate:new Date
        })
    }); 

In database dates are saved in same format i.e 2017-03-21

Snapey's avatar

In database dates are saved in same format i.e 2017-03-21

I smell a problem. What column type are they in the database?

Please or to participate in this conversation.