Laravel 可修订获取特定用户的所有修订列表

use*_*734 3 laravel revisionable laravel-5

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

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

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

cba*_*ier 5

我从来没有用过这个包。但是根据我所看到的,您应该能够将其添加到您的User模型中

public function revisions()
{
    return $this->hasMany(\Venturecraft\Revisionable\Revision::class)
}
Run Code Online (Sandbox Code Playgroud)

然后

@foreach($user->revisions as $history )
    <li> 
        {{ $user->first_name }} 
        changed {{ $history->fieldName() }} 
        from {{ $history->oldValue() }} 
        to {{ $history->newValue() }}
    </li>
@endforeach
Run Code Online (Sandbox Code Playgroud)

正如您在评论中所问:

但是我错过了在该列表中更改的实体。

(可选)我会为我的可修订模型实现一个接口,例如:

<?php
namespace App\Contracts;
interface RevisionableContract {
    public function entityName();
}
Run Code Online (Sandbox Code Playgroud)

然后在我所有使用 RevisionableTrait 的模型中:

<?php
namespace App\Models;
class MyModel extend Eloquent implements RevisionableContract {
    use RevisionableTrait;

    // (required)
    public function entityName(){
        return 'My Entity name';
    }
}
Run Code Online (Sandbox Code Playgroud)

最后 :

<?php
namespace App\Contracts;
interface RevisionableContract {
    public function entityName();
}
Run Code Online (Sandbox Code Playgroud)

historyOf() 可能会回来 false

您是否也知道如何使用用户的信息按降序列出所有修订版本?

从迁移文件,我可以看到它有created_atupdated_at时间戳。

你有两种可能性:

  1. 在您的view,您可以collection像这样直接订购它们:
<?php
namespace App\Models;
class MyModel extend Eloquent implements RevisionableContract {
    use RevisionableTrait;

    // (required)
    public function entityName(){
        return 'My Entity name';
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 当您为用户获得大量修订时,您可能会遇到性能问题,并且必须对它们进行分页。从你的controller,你将不得不对它们进行排序和分页它们在你的query,而不是collection
@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
Run Code Online (Sandbox Code Playgroud)