Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel eloquent where date is equal or greater than DateTime

I'm trying to fetch relational data from a model where the date column is higher or equal to the current time.

The date column is formated as this: Y-m-d H:i:s

What I'm trying to do is to grab all rows where the Y-m-d H:i:s is in the future.

Example: lets assume the date is 2017-06-01 and the time is 09:00:00 Then i would like got all rows where the date is in the future, and the time is in the future.

Currently my code looks like this, and it's almost working but it doesn't grab the rows where the date is the current day.

public function customerCardFollowups() {     return $this -> hasMany('App\CustomerCardFollowup', 'user_id') -> whereDate('date', '>', Carbon::now('Europe/Stockholm')) -> orderBy('date', 'asc') -> take(20); } 

What am I doing wrong? Thanks in advance.

like image 692
Kaizokupuffball Avatar asked May 30 '17 15:05

Kaizokupuffball


People also ask

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.

How can we get data between two dates using query in Laravel?

Try to do something like this: $date1 = Carbon::today()->toDateString(); $date2 = Carbon::today()->toDateString(); $myModel = MyModel::find(1); $myModel->whereBetween('created_at', [$date1, $date2]); $myModel->get(); Of course, you will need to change the dates.

What is the difference between eloquent and query builder in Laravel?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.


1 Answers

Sounds like you need to use >=, for example:

->whereDate('date', '>=', Carbon::now('Europe/Stockholm')) 
like image 200
Alexey Mezenin Avatar answered Sep 16 '22 18:09

Alexey Mezenin