'boolean', ]; public function ads() { return $this->hasMany(Ad::class); } public static function getActiveAds(string $key): Collection { $slot = static::query() ->where('key', $key) ->where('is_active', true) ->first(); if (!$slot) { return collect(); } $now = now(); $query = $slot->ads() ->where('is_active', true) ->where(function ($inner) use ($now) { $inner->whereNull('starts_at') ->orWhere('starts_at', '<=', $now); }) ->where(function ($inner) use ($now) { $inner->whereNull('ends_at') ->orWhere('ends_at', '>=', $now); }) ->orderBy('sort'); if ($slot->max_items) { $query->limit($slot->max_items); } $ads = $query->with('product')->get(); if ($ads->isNotEmpty()) { Ad::query()->whereIn('id', $ads->pluck('id'))->increment('view_count'); } return $ads; } }