When I run phpunit on my Laravel based project I get an error: ErrorException: Trying to get property of non-object
But only on phpunit, the page renders fine.
This is the line of code in my blade template that is de cause of the error:
{{ $user->roles->first()->display_name }}
When I replace this with the following (ugly) code the phpunit tests run ok:
@php ($role = preg_replace('/[^a-z\d ]/i', '', $user->roles->pluck('display_name')) )
{{ $role }}
The roles are eager loaded in the controller:
$users->load('roles');
Any idea why phpunit throws this error (and laravel not) and how to sove this in a better way?
Stacktrace from phpunit: https://paste.ee/p/dyMVs#