'get the date from database where date is 10 days ago with laravel
i need to get payments where the payment date is done before 100 days ago, I have a (date) when make a payment, and i tried this, but doesn't working:
$statusSearch = Payment::where('date', '<', strtotime('-10 Days'))->get();
Solution 1:[1]
You can use Carbon subDays() like below:
$statusSearch = Payment::where('date', '<=', Carbon::now()->subDays(10)->toDateTimeString())->get();
Solution 2:[2]
Let's use whereDate, because we need to compare dates, and Carbon to have DateInterval:
$tenDaysAgo = Carbon::now()->subDays(10);
$statusSearch = Payment::whereDate('date', '<', $tenDaysAgo)->get();
Then, don't forget to go in the Payment model and cast date column to date:
protected $casts = [
'date' => 'date',
];
Also I suggest you to rename this column to something else, just to now have issue in the near future during the query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Babo |
| Solution 2 | Cristian Cosenza |
