update(['status' => !$model->getOriginal('status')]); return redirect()->back()->with('success', __('The status has been changed')); } /** * Change model publication date + boolean * * @param Model $model * @return \Illuminate\Http\RedirectResponse * @throws \InvalidArgumentException Model must use IsPublishable trait. */ public function updatePublicationDate(Model $model) { if (!$this->isPublishable($model)) { throw new \InvalidArgumentException('Model must use IsPublishable trait'); } /** @var Publishable&Model $model */ if ($model->getStatus()) { $model->setUnpublishAt(now()); $model->setStatus(false); } else { $model->setPublishAt(now()); $model->setUnpublishAt(null); $model->setStatus(true); } $model->saveOrFail(); return redirect()->back()->with('success', __('The status has been changed')); } /** * Check if a Model implements Publishable interface. * * @param \Illuminate\Database\Eloquent\Model $model * * @return boolean */ protected function isPublishable(Model $model): bool { return $model instanceof Publishable && in_array(IsPublishable::class, class_uses_recursive($model)); } }