简体   繁体   中英

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!

Blade has the or syntax, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM