简体   繁体   中英

Eloquent, update all records where hours (time) difference between update_at and current time is greater than 24

I need to update all the records where update_at and current time have a difference greater than 24 hours. Actually I am updating the business as feature only for 24 hours after that businesses will automatically will be unsubscribed from feature. So I want to updated all the businesses like this.

 Business::where('is_featured', true)
        ->whereDate('updated_at', '<', now()) // need to calculate difference of 24 hours
        ->update(['is_featured' => false]);

Using carbon, substract 24 hours

Business::where('is_featured', true)
    ->whereDate('updated_at', '<', now()->subHours(24))
    ->update(['is_featured' => false]);

or subDay()

Business::where('is_featured', true)
    ->whereDate('updated_at', '<', now()->subDay())
    ->update(['is_featured' => false]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM