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

221
Visualizações
Lumen - Filter some results from all select methods

I have an expired_at column in database; that when creating any row, by default will be 5 minutes future of the creation time, and i want exclude rows which their expired_at has passed (littler from current time).

For example i have this rows in games table:

id creator_id created_at            updated_at            expire_at  
1  123456789  2017-04-26 01:13:22   2017-04-26 01:13:22   2017-04-26 01:18:22  
2  234567890  2017-04-26 01:16:06   2017-04-26 01:16:06   2017-04-26 01:21:06  

And when i fetch all rows with Game::all() or Game::where('condition') function at time 01:19:30, row 1 shouldn't be fetched, but row 2 should, because it still not expired.

I don't want to process expire time in target code, but in model code (for example overriding where function in model class)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can achieve this my the means of a (global) scope.

See also this link.

protected static function boot()
{
    parent::boot();
    static::addGlobalScope('not_expired', function (Builder $builder) {
        $builder->where('expired_at', '>', Carbon::now());
    });
}

Global scopes will be applied to all queries except when you disable them specifically.

Disabling them can be done by:

Game::withoutGlobalScope('not_expired')->all();
over 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