Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ORM, date compare

Tags:

php

orm

laravel

I'm using Laravel Framework.

I want all of the rows for this day. This is what I tried:

DB::table('users')->where('created_at', '>=', date('Y-m-d H:i:s')) 

(field created_at represented in database in format: Y-m-d H:i:s).

like image 202
user2096672 Avatar asked Feb 24 '13 15:02

user2096672


People also ask

How to compare date in Laravel Query?

Comparing date using raw query in laravel we will use DB::raw method to compare this. $users = DB::table('users') ->where('created_at', '2016-12-31') ->get(); //or $users = DB::table('users') ->where(\DB::raw('date("created_at")', '2021') ->get(); Comparing date in using operators like greater then equal to etc.

How do I use whereBetween in laravel?

The whereBetween() method is a query builder chained alongside other Laravel query builders used to fetch data from the database. The whereBetween() method queries the database table to fetch rows of records from the database within a range of values.


1 Answers

Hmmm...there was a good answer to this question which seems to have now disappeared.*

It was something like this:

User::where('created_at', '>=', new DateTime('today')) 

Note: if you're putting this code in a file with a namespace, or might use a namespace in the future, you should prefix the DateTime class with a backslash: new \DateTime('today').

* https://stackoverflow.com/questions/15052679/laravel-framework-how-to-get-today-queries

like image 64
Matt Browne Avatar answered Oct 01 '22 02:10

Matt Browne