【问题标题】:Laravel VentureCraft/revisionable returned name record with databseLaravel VentureCraft/revisionable 返回的名称记录与数据库
【发布时间】:2016-11-11 23:53:37
【问题描述】:

您好,在我的项目中,我使用 VentureCraft/revisionable https://github.com/VentureCraft/revisionable 这是记录数据库中记录更改历史的补充。一切正常,但是当我在页面上返回历史记录时,脚本以 id 的形式返回结果。这是我的表格“产品”products_table 这是表格“修订”revisions_table 此表格与表格相关联:文章、用户和类别。现在历史更改如下所示:

“用户 1 改名文章从 2 到 1”

我想把历史改成这样:

“用户 JACK 将姓名文章从 SHOES 更改为 BALL”

我尝试使用这个link

带有文件 Revisionable.php 的方法 identifiableName() 看起来像这样:

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

在模型 product.php 中我添加了:

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

或者这个:

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

但它不起作用。

【问题讨论】:

    标签: php laravel revision-history revisionable


    【解决方案1】:

    好的,我找到了解决这个问题的方法: 只使用: whereIdfindfaindOrFail 示例:

    article::whereId( $hist->old_value )->first()->article_name
    

    article::find( $hist->old_value )->article_name
    

    article::findOrFail( $hist->old_value )->article_name
    

    article:name 是文章表中的名称列。

    【讨论】:

      【解决方案2】:

      你可以使用

      $history->userResponsible()->first_name
      

      就像文档说的那样

      @foreach($resource->revisionHistory as $history)
        @if($history->key == 'created_at' && !$history->old_value)
          <li>{{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}</li>
        @else
          <li>{{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
        @endif
      @endforeach
      

      或者如果您想将其用作响应 api,您可以操纵响应以获取它们的关系

      $response = $question->revisionHistory->map(function($history) {
                  return ['history' => $history, 'users' => $history->userResponsible()];
              });
      return response()->json($response);
      

      【讨论】:

        猜你喜欢
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        • 2018-03-23
        • 2017-10-15
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        相关资源
        最近更新 更多