Search code examples
laravellaravel-5revisionable

Laravel revisionable getting a list of all revisions by specific user


I'm using the VentureCraft/revisionable-package and it shows me in the ReadMe how to show the Revisions of a Model that has revisions:

@foreach($account->revisionHistory as $history )
    <li> 
         {{ $history->userResponsible()->first_name }} 
         changed {{ $history->fieldName() }} 
         from {{ $history->oldValue() }} 
         to {{ $history->newValue() }}
    </li>
@endforeach

But I want a list of All revisions that are done by a Specific user; how to achieve that? So I can show a History of Revisions that are done by one specific user.


Solution

  • I have never used this package. But based on what I see, you should be able to add this in your User model

    public function revisions()
    {
        return $this->hasMany(\Venturecraft\Revisionable\Revision::class)
    }
    

    then

    @foreach($user->revisions as $history )
        <li> 
            {{ $user->first_name }} 
            changed {{ $history->fieldName() }} 
            from {{ $history->oldValue() }} 
            to {{ $history->newValue() }}
        </li>
    @endforeach
    

    As you asked in the comments :

    But I'm missing the Entity that's changed in that list.

    (optional) I would implement an interface to my revisionable models with something like :

    <?php
    namespace App\Contracts;
    interface RevisionableContract {
        public function entityName();
    }
    

    Then in all my models that use the RevisionableTrait :

    <?php
    namespace App\Models;
    class MyModel extend Eloquent implements RevisionableContract {
        use RevisionableTrait;
    
        // (required)
        public function entityName(){
            return 'My Entity name';
        }
    }
    

    Finally :

    @foreach($user->revisions as $history )
        <li> 
            {{ $user->first_name }} 
            changed {{ $history->fieldName() }} 
            from {{ $history->oldValue() }} 
            to {{ $history->newValue() }}
            on the entity {{ $history->historyOf()->entityName() }}
        </li>
    @endforeach
    

    historyOf() may return false

    Do you also have an idea how I can make a list of all revisions in desc-order with that info of the user?

    From the migrations file, I can see that it has created_at and updated_at timestamps.

    You have two possibilities :

    1. In your view, you can directly order them on your collection like this :
    @foreach($user->revisions->sortByDesc('created_at') as $history )
    
    1. When you get a lot of revisions for a user, you will probably have performance issues and you will have to paginate them. From your controller, you will have to sort them and paginate them in your query instead of the collection.
    public function index()
    {
        $user = User::find(1);
        $revisions = $user->revisions()->orderBy('created_at')->paginate(15);
        return view('your.view', compact('user', 'revisions'));
    }