I have invoices table where I have date and pay_deadline. Pay deadline represent number of days from date of invoice that you have a time for pay. For example date=09/01/2024 and pay_deadline=7 that means that you have 7 days from 09/01/2024 to pay.
My question is:
How can I query for example all invoices that are still in time slot to pay? That means now() is between date and date + pay_deadline
To solve this problem, you can use the whereRaw method to compare the current date with the date and date + pay_deadline in your query. Here's how you can do it:
use Carbon\Carbon;
$now = Carbon::now();
Invoice::query()
->whereRaw('? BETWEEN `date` AND DATE_ADD(`date`, INTERVAL `pay_deadline` DAY)', [$now])
->get();
Explanation:
Carbon::now() gets the current date and time.
whereRaw allows you to write a raw SQL query.
? BETWEEN date AND DATE_ADD(date, INTERVAL pay_deadline DAY) checks if the current date ($now) is between the date of the invoice and the date plus the pay_deadline days.
This query will return all invoices that are still within the time slot to pay.