我从来没有用过这个包。但根据我所看到的,你应该可以在你的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
正如您在评论中所问的那样:
但我错过了该列表中更改的实体。
(可选)我会为我的可修改模型实现一个接口,例如:
<?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
您是否也知道如何使用用户的信息按降序列出所有修订?
从迁移文件中,我可以看到它有created_at
时间戳updated_at
。
你有两种可能:
- 在您的
view
中,您可以collection
像这样直接订购它们:
@foreach($user->revisions->sortByDesc('created_at') as $history )
- 当您为用户获得大量修订时,您可能会遇到性能问题,您将不得不对它们进行分页。从您的
controller
, 您将不得不对它们进行排序并在您的query
而不是collection
.
public function index()
{
$user = User::find(1);
$revisions = $user->revisions()->orderBy('created_at')->paginate(15);
return view('your.view', compact('user', 'revisions'));
}