Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

444
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda