M J
M J

Reputation: 2991

Query in Laravel not working against created_at column

I am writing a query in which I want to fetch data against created_at column. But its not working, it does not return anything. I am using Mongodb as database. Created_at column date is in the format "created_at": ISODate("2016-05-11T09:29:33.112Z") and when I fetch it from database it gives me 2016-05-11 09:29:33.

$datetimenow = date('Y-m-d H:i:s');
$data = ChatMessages::where('created_at','<',$datetimenow)->count();

Upvotes: 0

Views: 330

Answers (2)

M J
M J

Reputation: 2991

I got the solution, this is the way how I can do this.

$d1 = new MongoDate(strtotime('2016-01-04 23:21:46'));
$d2 = new MongoDate(strtotime('2016-05-09 23:21:46'));
$data = ChatMessages::whereRaw(['created_at' => array('$gt' => $d1, '$lt' => $d2)])->count();

Upvotes: 0

Rehan Manzoor
Rehan Manzoor

Reputation: 2753

Try using Carbon Package

$now = \Carbon\Carbon::now('Asia/Dubai'); //you can specify your own timezone
$messages = ChatMessages::where('created_at', '<', $now)->get();
dd($messages);

Upvotes: 1

Related Questions