Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

447
Vistas
Check if date has expired Laravel Date Field Type SQL

In my application I have to check whether or not a certain date has expired, when so a e-mail has to be send out. The date's are saved in the Clients table. How can I add 5 days to the retrieved date, so I can check whether or not it will be expired in 5 days? The function which I have until now.

public function handle() {

    $users =\App\Client::select('subscription_ends_at')
        ->groupBy('subscription_ends_at')
        ->get();

    $data_now = Carbon::now();

    foreach($date_expired as $users ){
        // $date_expired + 5 days

        if($date_expired > $data_now){
            //Send e-mail
        }
    }
}
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You should make your comparision part of your query by using whereDate(). There's no benefit in your case for filtering this data on PHP side:

$now = Carbon::now()->addDays(5);

$users =\App\Client::select('subscription_ends_at')
    ->groupBy('subscription_ends_at')
    ->whereDate('date_expired', '>', $now->toDateString()), 
    ->get();

Docs on Eloquent where clauses here.

EDIT

Your query is broken for me. Using groupBy() will make it return only a fraction of clients grouping those expiring the same day into one entry. This in result defeats the purpose of sending notifications as not everyone will get it, so I'd rather remove groupBy() completely.

about 4 years ago · Santiago Trujillo Denunciar

0

Use addDays() to add days:

Carbon::now()->addDays(5);

Also, it's always a good idea to add this to the query instead of loading all the data and filter collection:

->where('subscription_ends_at', '>', Carbon::now()->addDays(5))

This will work if you'll add subscription_ends_at to the $dates property.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda