3

我正在使用VentureCraft/revisionable -package,它在自述文件中向我展示了如何显示具有修订的模型的修订:

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

但我想要一个由特定用户完成的所有修订的列表;如何做到这一点?所以我可以显示一个特定用户完成的修订历史。

4

2 回答 2

5

我从来没有用过这个包。但根据我所看到的,你应该可以在你的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

你有两种可能:

  1. 在您的view中,您可以collection像这样直接订购它们:
@foreach($user->revisions->sortByDesc('created_at') as $history )
  1. 当您为用户获得大量修订时,您可能会遇到性能问题,您将不得不对它们进行分页。从您的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'));
}
于 2018-09-21T09:54:17.053 回答
-1

我不能使用那个包,但它似乎很容易理解。如果您可以显示用户的历史记录,则应将其添加到您的“用户”实体中:

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

或者,如果您想过滤特定的可变形实体,您应该这样做:

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

我认为该答案与您想做的事情相对应。

于 2018-09-21T10:15:41.277 回答