Home > OS >  get the date from database where date is 10 days ago with laravel
get the date from database where date is 10 days ago with laravel

Time:03-24

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();

CodePudding user response:

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.

CodePudding user response:

You can use Carbon subDays() like below:

$statusSearch = Payment::where('date', '<=', Carbon::now()->subDays(10)->toDateTimeString())->get();
  • Related