Reputation: 801
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.
Upvotes: 3
Views: 2020
Reputation: 6058
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 :
view
, you can directly order them on your collection
like this : @foreach($user->revisions->sortByDesc('created_at') as $history )
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'));
}
Upvotes: 5
Reputation: 908
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.
Upvotes: -1