each(function (string $model) { /** @var \Illuminate\Database\Eloquent\Collection */ $modelListToPublish = $model::query() ->whereNotNull('publish_at') ->where('publish_at', '<=', Carbon::now()) ->where(function ($query) { $query->whereNull('unpublish_at') ->orWhere('unpublish_at', '>', Carbon::now()); }) ->where('published', false) ->get(); Log::debug(__CLASS__ . " models to publish " . $modelListToPublish->count()); $modelListToPublish->each(function ($currentModel) { $currentModel->published = true; $currentModel->saveOrFail(); }); /** @var \Illuminate\Database\Eloquent\Collection */ $modelListToUnpublish = $model::query() ->whereNotNull('unpublish_at') ->where('unpublish_at', '<=', Carbon::now()) ->where('published', true) ->get(); Log::debug(__CLASS__ . " models to unpublish " . $modelListToUnpublish->count()); $modelListToUnpublish->each(function ($currentModel) { $currentModel->published = false; $currentModel->saveOrFail(); }); }); } }