I'm having an issue here where something works just fine on the first 10 results, but when I click on the second pagination, or if I perform a sort that brings the record in question into display, it causes an issue. It basically is a where request to display another record on the same table, and it works with the other records.
I have created different records where they work fine if they are in the first 10 results, but the moment I change them to appear after the pagination break, I get this same error. Otherwise, it works fine if the field is null.
The error it is giving me is that it appears as a non-object, but the code works with every other similar record with the same object selected.
Error
ErrorException (E_ERROR)
Trying to get property 'full_name' of non-object (View: resources/views/doctors/index.blade.php)
<td><?php echo e($doctors->where('id', $doctor->ffs_id)->first()->full_name); ?></td>
Code
@if ($doctor->ffs_id == null)
    <td></td>
@else
    <td>
        {{ $doctors->where('id', $doctor->ffs_id)->first()->full_name }}
    </td>
@endif
Pagination Code: Blade
{{ $doctors->appends([
    'search' => $search,
    'sortColumn' => $sortColumn,
    'sortDirection' => $sortDirection
])->links() }}
Pagination Code: Controller
// Searches, sorts, and filters
$approved = $request->approved;
$search = $request->search;
$sortColumn = ($request->sortColumn == null ? 'last_name' : $request->sortColumn);
$sortDirection = ($request->sortDirection == null ? 'asc' : $request->sortDirection);
$doctors = Doctor::
    when($search, function ($query) use ($search) {
        $query->where(function ($query) use ($search) {
            $query
                ->where('first_name', 'LIKE', '%' . $search . '%')
                ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                ->orWhere('type', 'LIKE', '%' . $search . '%')
                ->orWhere('npi', 'LIKE', '%' . $search . '%')
                ->orWhere('license', 'LIKE', '%' . $search . '%')
                ->orWhere('dea', 'LIKE', '%' . $search . '%');
        });
    })
    ->when($approved, function ($query) use ($approved) {
        $query->where(function ($query) use ($approved) {
            $query->where('is_approved', $approved);
        });
    })
    ->orderBy($sortColumn, $sortDirection)
    ->paginate(10);
Edit: Even removing all of the sort functionality and attempting a load of /doctors?page=2 gives the same error.
Edit 2: I've confirmed that it has something to do with how $doctors is written in the controller. If I create a version of Doctor::all() and reference it in the blade, it pulls up with no errors.
 
    