From e168efa460cbc49971874106233630726b875399 Mon Sep 17 00:00:00 2001 From: Shea Date: Wed, 23 Oct 2024 15:17:25 +1300 Subject: [PATCH] move draftRecord HasOne to HasRevisor Trait --- src/Concerns/HasPublishing.php | 19 ------------------- src/Concerns/HasRevisor.php | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Concerns/HasPublishing.php b/src/Concerns/HasPublishing.php index 6317f10..be4ee25 100644 --- a/src/Concerns/HasPublishing.php +++ b/src/Concerns/HasPublishing.php @@ -192,25 +192,6 @@ public function publishedRecord(): HasOne ); } - /** - * Get the draft record for this model - * - * @throws Exception - */ - public function draftRecord(): HasOne - { - if ($this->isDraftTableRecord()) { - throw new Exception('The draft record HasOne relationship is only available to Published and Version records'); - } - - $instance = (new static)->withDraftContext(); - $localKey = $this->isVersionTableRecord() ? 'record_id' : $this->getKeyName(); - - return $this->newHasOne( - $instance, $this, $instance->getModel()->getTable().'.'.$this->getKeyName(), $localKey - ); - } - /** * Get the publisher relationship for this model */ diff --git a/src/Concerns/HasRevisor.php b/src/Concerns/HasRevisor.php index c8a3ccd..e17f9db 100644 --- a/src/Concerns/HasRevisor.php +++ b/src/Concerns/HasRevisor.php @@ -4,7 +4,9 @@ namespace Indra\Revisor\Concerns; +use Exception; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; use Indra\Revisor\Contracts\HasRevisor as HasRevisorContract; @@ -92,6 +94,25 @@ public function getDraftTable(): string return Revisor::getDraftTableFor($this->getBaseTable()); } + /** + * Get the draft record for this model + * + * @throws Exception + */ + public function draftRecord(): HasOne + { + if ($this->isDraftTableRecord()) { + throw new Exception('The draft record HasOne relationship is only available to Published and Version records'); + } + + $instance = (new static)->withDraftContext(); + $localKey = $this->isVersionTableRecord() ? 'record_id' : $this->getKeyName(); + + return $this->newHasOne( + $instance, $this, $instance->getModel()->getTable().'.'.$this->getKeyName(), $localKey + ); + } + /** * Get a Builder instance for the Draft table */