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

标签 laravel laravel-5 revisionable

我正在使用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_atupdated_at 时间戳。

你有两种可能性:

  1. 在您的 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/

    相关文章:

    php - 未找到特征 'Venturecraft\Revisionable\RevisionableTrait'

    php - Laravel 查询生成器使用 MySQL 中的变量进行复杂选择?

    laravel 登录不适用于 cloudFront AWS 和证书管理器

    laravel - 如何在 laravel 5 中获取特定组的路线列表?

    laravel - Composer 更新未安装整个 Laravel 应用程序

    php - 分形 laravel 变压器中的条件属性

    php - Laravel 5 在 Blade 中回显包含 html 的 session 变量

    laravel - 向 Revisionable 显式提供 "user_id"

    laravel - 如何在 jwt-auth 中获取授权用户的 ID?

    laravel - 队列上的连接名称