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

220
Vistas
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 Respuestas
Responde la pregunta

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 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