Tuesday, January 25, 2022

[FIXED] Search with date range in laravel

Issue

I have a db table dateprices

id|tour_id|start|end|price

I want to get all the price of the tour starting and ending between the dates i.e. 2017-06-20 and 2017-07-11

I have written following query but it is returning an empty collection:

$dates = DatePrice::where('tour_id','=',$request->product_id)
  ->where('start', 'like', '%'.$request->start.'%')
  ->where('end', 'like', '%'.$request->end.'%')
  ->get();

Is there something wrong with my query ?


Solution

You should use the whereBetween() function like so:

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


Answered By - Florian Humblot

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.