diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 7099ef50..ece66ef4 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -138,11 +138,13 @@ public function postSave() $changes_to_record = $this->changedRevisionableFields(); $revisions = array(); - + $uniqid = uniqid(); + foreach ($changes_to_record as $key => $change) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $key, 'old_value' => array_get($this->originalData, $key), 'new_value' => $this->updatedData[$key], @@ -175,9 +177,11 @@ public function postCreate() if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => self::CREATED_AT, 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, @@ -200,9 +204,12 @@ public function postDelete() if ((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->isSoftDelete() && $this->isRevisionable($this->getDeletedAtColumn())) { + + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $this->getDeletedAtColumn(), 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index f298dcf7..1e62a466 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -93,6 +93,27 @@ public function revisionHistory() return $this->morphMany('\Venturecraft\Revisionable\Revision', 'revisionable'); } + /** + * @return mixed + */ + public function revisionHistoryMapped() + { + $out = []; + $data = $this->revisionHistory()->get()->sortByDesc(function ($item) { + return $item->created_at; + }); + foreach ($data as $key => $item) { + $out[$item->revision_id]['item'] = $item; + $out[$item->revision_id]['revision_data'][] = [ + 'key' => $item->key, + 'old_value' => $item->oldValue(), + 'new_value' => $item->newValue() + ]; + } + + return $out; + } + /** * Generates a list of the last $limit revisions made to any objects of the class it is being called from. * @@ -173,11 +194,13 @@ public function postSave() $changes_to_record = $this->changedRevisionableFields(); $revisions = array(); - + $uniqid = uniqid(); + foreach ($changes_to_record as $key => $change) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $key, 'old_value' => array_get($this->originalData, $key), 'new_value' => $this->updatedData[$key], @@ -217,9 +240,11 @@ public function postCreate() if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => self::CREATED_AT, 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, @@ -244,9 +269,11 @@ public function postDelete() && $this->isSoftDelete() && $this->isRevisionable($this->getDeletedAtColumn()) ) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $this->getDeletedAtColumn(), 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, diff --git a/src/migrations/2017_02_23_062329_update_revisions_table.php b/src/migrations/2017_02_23_062329_update_revisions_table.php new file mode 100644 index 00000000..4bb9cbbe --- /dev/null +++ b/src/migrations/2017_02_23_062329_update_revisions_table.php @@ -0,0 +1,30 @@ +string('revision_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('revisions', function ($table) { + $table->dropColumn('revision_id'); + }); + } +}