Solution:
First,
isset($record) will always return true since you’re setting the $record variable yourself.
Second,
Instead of !empty($record), you should use either !$record->isEmpty() or $record->isNotEmpty().
Third,
The null values you encounter are probably a result of the left join. Use an (inner) join if you want to avoid null values.