Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!