我正在使用VentureCraft/revisionable -package,它在自述文件中向我展示了如何显示具有修订的模型的修订:
@foreach($account->revisionHistory as $history )
<li>
{{ $history->userResponsible()->first_name }}
changed {{ $history->fieldName() }}
from {{ $history->oldValue() }}
to {{ $history->newValue() }}
</li>
@endforeach
但我想要一个由特定用户完成的所有修订的列表;如何实现这一目标?因此我可以显示由一个特定用户完成的修订历史记录。
最佳答案
我从来没有使用过这个包。但根据我所看到的,您应该能够将其添加到您的 User
模型
public function revisions()
{
return $this->hasMany(\Venturecraft\Revisionable\Revision::class)
}
然后
@foreach($user->revisions as $history )
<li>
{{ $user->first_name }}
changed {{ $history->fieldName() }}
from {{ $history->oldValue() }}
to {{ $history->newValue() }}
</li>
@endforeach
正如您在评论中所问的:
But I'm missing the Entity that's changed in that list.
(可选)我将通过以下方式实现可修订模型的接口(interface):
<?php
namespace App\Contracts;
interface RevisionableContract {
public function entityName();
}
然后在我所有使用 RevisionableTrait 的模型中:
<?php
namespace App\Models;
class MyModel extend Eloquent implements RevisionableContract {
use RevisionableTrait;
// (required)
public function entityName(){
return 'My Entity name';
}
}
最后:
@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()
可能返回 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?
从迁移文件中,我可以看到它具有 created_at
和 updated_at
时间戳。
你有两种可能性:
- 在您的
View
中,您可以直接在集合
中订购它们,如下所示:
@foreach($user->revisions->sortByDesc('created_at') as $history )
- 当您收到用户的大量修订时,您可能会遇到性能问题,并且必须对它们进行分页。在您的
Controller
中,您必须对它们进行排序并在您的查询
(而不是集合
)中对它们进行分页。
public function index()
{
$user = User::find(1);
$revisions = $user->revisions()->orderBy('created_at')->paginate(15);
return view('your.view', compact('user', 'revisions'));
}
关于Laravel revisionable 获取特定用户的所有修订的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52386277/