Search code examples
phplaravellaravel-5eloquentrevisionable

Obtaining field from table using primary key


Using Laravel and Revisionable Package for tracking changes. In my view I'm populating my table:

@foreach($revisions as $revision)
      @if($revision->key == 'created_at' && !$revision->old_value)
      <tr>
        <td>{{ $revision->revisionable_type }}</td>
        <td>{{ $revision->revisionable_id }}</td>
        <td width="50">{{ $revision->userResponsible()->first_name }}</td>
        <td Width="50"></td>
        <td></td>
        <td>{{ $revision->newValue() }}</td>
        <td width="150">{{ $revision->created_at }}</td>
      </tr>  
      @else
      <tr>
        <td>{{ $revision->revisionable_type }}</td>
        <td>{{ $revision->revisionable_id }}</td>
        <td width="50">{{ $revision->userResponsible()->first_name }}</td>
        <td width="50">{{ $revision->fieldName() }}</td>
        <td>{{ $revision->oldValue() }}</td>
        <td>{{ $revision->newValue() }}</td>
        <td width="150">{{ $revision->updated_at }}</td>
      </tr>  
      @endif 
@endforeach

The second column {{ $revision->revisionable_id }} happens to be the primary key of the user record that was modified. I would like to return the email address or first_name/last_name of that users record. I'm fairly new at this and could use a push in the right direction!

Thanks!


Solution

  • You can access the model that given revision relates to by accessing the revisionable relation of that revision. In your case, in order to display the email property of related model, you could do the following:

    <td>{{ $revision->revisionable->email }}</td>