Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joshblevins's avatar

Query to find x days from date with X in column

I am not sure if this is even possible but I have a table like this below.

Estimates: created_at updated_at service_date-> Date Service was completed nextServiceDays -> total days before next service email is sent. nextServiceEmail -> Date Email sent status

I am trying to write a query that returns all estimates with a status of 4 and nextServiceEmail is null that is greater or = to nextServiceDays.

Here is what I have but I am not sure how to incorporate the nextServiceDays.

$estimates = Estimate::
whereNull('nextServiceEmail')
//Feel Like I should do something with the date here
->where(status, 4)
->get()
0 likes
2 replies
laracoft's avatar

@joshblevins

... that is greater or = to nextServiceDays.

  1. What is greater or = to nextServiceDays?
  2. Can you produce the SQL you want and we convert to eloquent for you?
joshblevins's avatar

In all honestly, with the value, I need being in the each specific row I am not sure how to use it inside a query. Here is the only thing I can think to do and not sure if it is the best approach.


 
$estimates = Estimate::
whereNull('nextServiceEmail')
//Feel Like I should do something with the date here
->where(status, 4)
->get()

foreach ($estimates as $row)
{
	$date = Carbon::today()->subDays($row->nextServiceDays);

	if(Carbon::now() <= $date ){

		//Complete notification and update record.

	}
}

Please or to participate in this conversation.