record($request); $this->authorize('view', $record); $revisions = $record->revisions() ->with('user') ->when($request->filled('event'), fn ($q) => $q->where('event', $request->string('event'))) ->paginate(perPage: min($request->integer('per_page', 25), 100)) ->withQueryString(); return RevisionResource::collection($revisions); } /** * Put back what one revision changed, as the panel's history does. * Restoring is itself a change, so it lands in the history too — the way * back is never lost. */ public function restore(Request $request, Revision $revision): RevisionResource { $model = Relation::getMorphedModel($revision->revisionable_type) ?? $revision->revisionable_type; $record = AdminContent::findOf($model, $revision->revisionable_id); $this->authorize('update', $record); $values = $revision->previousValues(); if ($values === []) { throw ValidationException::withMessages(['revision' => [__('rules.revisions.nothing_to_restore')]]); } // Undoing a change can publish or unpublish, and that stays with whoever // may publish. $guarded = array_intersect(array_keys($values), AdminContent::publicationFields($record)); if ($guarded !== [] && $request->user()->cannot($record->getTable().'.publish')) { throw ValidationException::withMessages(['revision' => [__('rules.publication.forbidden')]]); } // Only what the model lets a form write: a revision may mention // bookkeeping columns, and those are not anybody's to put back. $record->forceFill(array_intersect_key($values, array_flip($record->getFillable())))->save(); return RevisionResource::make($record->revisions()->with('user')->first()); } }