Search code examples
laravelrevisionable

Laravel with Revisionable Package and catching errors


Using Laravel and Revisionable package. I am populating a table with user record modifications and have the following code snippet:

<tr>
    <td width="150">{{ $revision->updated_at }}</td>
    <td>User</td>
    <td>{{ $revision->revisionable->first_name }} {{ $revision->revisionable->last_name }}</td>
    <td width="50">{{ $revision->fieldName() }}</td>
    <td width="50">{{ $revision->userResponsible()->first_name }}</td>
    <td>{{ $revision->oldValue() }}</td>
    <td>{{ $revision->newValue() }}</td>
</tr> 

Getting the users first and last name works fine when the original user record exists but in some cases the original record (user) is deleted and it causes an error because the user record no longer exists. Is there an IF Statement I can use to first check if the first name field returns an error? Then I can display first/last name for records that still exist and something else if it doesn't exist.

Thanks in advance!


Solution

  • Blade has the orsyntax, really nice:

    {{ $revision->revisionable->first_name or 'N/A'}} 
    {{ $revision->revisionable->last_name or 'N/A'}}
    

    alternatives, though not as elegant:

    {{ isset($revision->revisionable->first_name) ? $revision->revisionable->first_name : 'N/A' }}
    

    or

    @if (isset($revision->revisionable->first_name))
        {{ $revision->revisionable->first_name }}
    @else
        N/A
    @endif