Are you using database sessions in testing? What is the value of SESSION_DRIVER in your phpunit.xml?
Jun 30, 2026
4
Level 7
Using actingAs while using database session
I'm using Laravel, InertiaJS and Fortify.
Here's the User model
use App\Models\Session;
//...
public function sessions(): HasMany {
return $this->hasMany(Session::class);
}
public function last_activity() {
return $this->sessions()->orderByDesc('last_activity')->first()->last_activity;
}
HandleInertiaRequest
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'auth' => [
// ...
'last_activity' => auth()->check() ? auth()->user()->last_activity() : null,
],
]);
}
I'm getting this output Attempt to read property "last_activity" on null. I noticed that even when auth()->login($testUser) or actingAs() is used, it does not record the session.
So, I think of doing this in User model:
if($this->sessions->count()) {
return $this->sessions()->orderByDesc('last_activity')->first()->last_activity;
}
Would this suffice?
Please or to participate in this conversation.