Skip to content

Commit

Permalink
added single field history method
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Sep 10, 2018
1 parent 70bd40a commit bd2fc5f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Laravel/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sofa\Revisionable\Laravel;

use Carbon\Carbon;
use Illuminate\Support\Collection;

trait Revisionable
{
Expand Down Expand Up @@ -229,4 +230,27 @@ public function getRevisionPresenter()
? $this->revisionPresenter
: Presenter::class;
}

/**
* Get all updates for a given field.
*
* @param string $field
* @return Illuminate\Support\Collection
*/
public function getFieldHistory(string $field) : Collection
{
return $this->revisions->map(function ($revision) use ($field) : ?array {
if ($revision->old($field) == $revision->new($field)) {
return null;
}

return [
'created_at' => (string) $revision->created_at,
'user_id' => $revision->executor->id ?? null,
'user_email' => $revision->executor->email ?? null,
'old' => $revision->old($field),
'new' => $revision->new($field),
];
})->filter()->values();
}
}

0 comments on commit bd2fc5f

Please sign in to comment.