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

Thursday, February 10, 2022

[FIXED] is whereBetween in laravel inclusive?

 February 10, 2022     laravel, laravel-5, mysql, php     No comments   

Issue

In my database there are 4 students:

1st Student's created_at = 2016-05-12 02:23:51

2nd Student's created_at = 2016-05-27 07:37:45

3rd Student's created_at = 2016-05-29 07:40:29

4th Student's created_at = 2016-05-29 07:50:05

Why does my code only returns the 1st student?

$students = Student::select('id as ID_NO', 'fname as Firstname', 'lname as Lastname', 'created_at')
                             ->whereBetween('created_at', 
                              [Carbon::createFromDate(2016, 5, 12)->toDateString(),
                               Carbon::createFromDate(2016, 5, 27)->toDateString()])

The 1st and 2nd student should be returned. Is the "to" part inclusive in whereBetween or there's something wrong with my code ?

I need your help guys. Thanks in advance!


Solution

You need to format your created_at to Y-m-d format. Please see the change:

$students = Student::select('id as ID_NO', 'fname as Firstname', 'lname as Lastname', 'created_at')
                                 ->whereBetween(DB::raw('date(created_at)'), 
                                  [Carbon::createFromDate(2016, 5, 12)->toDateString(),
                                   Carbon::createFromDate(2016, 5, 27)->toDateString()])


Answered By - Ali
  • 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