【问题标题】:How to use VentureCraft/revisionable in Laravel?如何在 Laravel 中使用 VentureCraft/revisionable?
【发布时间】:2016-04-20 20:30:50
【问题描述】:

我想知道如何在 CRUD 应用程序中使用 VentureCraft/revisionable 并获取历史记录

例如:用户每天都需要添加里程和编辑里程......在我看来(revision.blade.php)我想获取我尝试过的添加和编辑的历史记录,但我不知道如何下一步?

这是模特

class Mileage extends Model
{
    use \Venturecraft\Revisionable\RevisionableTrait;
    protected $guarded = [];
}

这是路线

Route::get('/history', function () {
    $mileage = \App\Mileage::all();
    $history = $mileage->revisionHistory;
    return view('revision',compact('history'));
});

我不知道用什么代码来查看????

mileage 有 user_id、date、mileage 我想这样显示

Ex : 用户 ...A... 已为 ....date.. 添加里程数为 ...mileage...

如果他们编辑我想显示这样的用户 ...A... 更新里程 .....date.... 值是 ...mileage... 到 ...mileage...我该怎么做?

【问题讨论】:

  • 没有在 Laravel 5.5 上工作有什么想法吗?

标签: laravel revisionable


【解决方案1】:

在显示历史之前。首先,可修订库假定在您的 Mileage 模型中,调用了用户模型的关系方法 user() 因为 milage 表中的 FK 是 user_id。您还需要将以下代码添加到您的 User 模型和 Mileage 模型中。

public function identifiableName(){
    return $this->name;
}

在您看来,您需要像这样遍历历史数组。

@foreach($history as $h )
     <li>{{ $h->userResponsible()->first_name }} has updated milage  {{ $h->fieldName() }} from {{ $h->oldValue() }} to {{ $h->newValue() }}</li>
@endforeach

【讨论】:

    【解决方案2】:
    Route::get('/history', function () {
        $mileage = \App\Mileage::all();
        return view('revision',compact('mileage'));
    });
    

    现在在您的 blade 文件中

      @foreach($mileage->revisionHistory as $history )
        <p class="history">
          {{ $history->created_at->diffForHumans() }}, {{ $history->userResponsible()->name }} changed
          <strong>{{ $history->fieldName() }}</strong> from
          <code>{{ $history->oldValue() }}</code> to <code>{{ $history->newValue() }}</code>
        </p>
      @endforeach
    

    现在,如果您需要 controller 或任何其他地方的历史记录,您必须调用使用 RevisionableTrait 特征的 model 实例的 revisionHistory

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 2016-11-11
      • 2018-03-23
      • 2017-05-17
      • 2023-03-16
      • 2018-03-08
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多