简体   繁体   中英

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.

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'));
}

I could not use that package but it seems quite easy to understand. If you can show history of a user you should add this to your "User" entity:

public function history()
{
    return $this->hasMany(\Venturecraft\Revisionable\Revision::class, 'user_id', 'id');
}

Or if you want to filter a specific morphable entity you should do it like this:

public function historyForUser(User $user)
{
    return $this->morphMany(\Venturecraft\Revisionable\Revision::class, 'revisionable')->where('user_id' , '=', $user->getKey())->getResults();
}

I think that answer is corresponded for what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM