PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] Search with date range in laravel

 January 25, 2022     laravel, laravel-5.4, mysql, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing