Laravel search for records 7 days old only
Asked Answered
L

3

12

i am trying to pull records that are 7 days old only , not older or earlier. But its not working, i'm using Carbon.

->where(DB::raw('date(AppDate)'), Carbon::now()->subDays(7))
Loveinidleness answered 13/10, 2017 at 13:37 Comment(0)
L
8

I have a solution, but its not using Carbon.

 ->whereRaw('DATE(AppDate) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
Longwise answered 13/10, 2017 at 14:16 Comment(0)
D
28

You can use whereDate for that :

->whereDate('created_at', Carbon::now()->subDays(7))
->get();

In the documentation :

The whereDate method may be used to compare a column's value against a date

PS : Since Laravel 5.3

Delmerdelmor answered 13/10, 2017 at 14:15 Comment(2)
@Marabox i forgot to mention i am using Laravel 5.0Loveinidleness
it's ok it will hepl others witch uses laravel 5.3 or newer one ;)Delmerdelmor
L
8

I have a solution, but its not using Carbon.

 ->whereRaw('DATE(AppDate) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
Longwise answered 13/10, 2017 at 14:16 Comment(0)
C
-1

To sum my last 7 days of records:

$date = \Carbon\Carbon::today()->subDays(7);
$Profitinsevendays = DB::table('n_profit_loss')->where('datetime', '>=', $date)->sum('profit_or_loss');
Chick answered 28/2, 2018 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.