Code Demon Code Demon - 1 year ago 150
MySQL Question

whereBetween eloquent query returns empty output

I'm trying to use 'whereBetween' eloqouent query with two given start and end dates.

$first_day_this_month = date('Y-m-01 H:s:i'); //get the first day of the current month
$yesterDay = date('Y-m-d H:s:i',strtotime("-1 days")); //get yesterday's date

$d = m_chat_history::where('employee_id',$request->other_id)

I make sure I have all the required data for the query by 'var_dump' and it did gives me all the required data needed for the query but the query returns me an empty output. Any ideas, clues, suggestions, help, recommendations please? I tried to remove the 'whereBetween' and my query works like it returns me the expected output but with 'whereBetween', the return output is empty.

Answer Source

Make sure to use the same type and format as created_at when defining the values for whereBetween. As you're using datetime you could define edge values like (just one of many ways of doing it):

$first_day_this_month = date('Y-m-01 H:i:s');
$yesterday = date('Y-m-d H:i:s', strtotime("-1 day"));

Also make sure of the order of params (still please consider edge cases like first day of the month where $yesterday would be smaller, so you have to add some logic and be careful):

->whereBetween('created_at', [$first_day_this_month, $yesterday])

Edit: wasn't timestamp...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download