Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matttonks11's avatar

Trying to get property of non-object auth()->user()->subjects()

Hi there,

I'm currently trying to test some middleware I created for my application. However i'm currently getting the error message...

ErrorException: Trying to get property 'subjects' of non-object (View: C:\xampp\htdocs\educatum\resources\views\home.blade.php)


use RefreshDatabase;

public function setUp() : void
{
        parent::setUp();

        Role::create(['name' => 'student']);
        Role::create(['name' => 'teacher']);
        Role::create(['name' => 'admin']);
}

/** @test */
public function a_student_can_access_student_pages()
{
    $this->withoutExceptionHandling();

    //create user
        $user = factory('App\User')->create();

    //assign them as a student
        $user->assignRole('student');


    //Sign them in
        $this->actingAs($user);

       
    Assert the student home page loads correctly
        $this->get('/students/home')->assertStatus(200);
}

subjects is a scope on the User model to fetch subjects for a signed in user.

The weird thing is there is no issue in the browser and everything works fine. If I removed the call to subjects in the blade file the test passes.

I'm just unsure as to why this affects the test, can anybody provide some help?

0 likes
1 reply
matttonks11's avatar
matttonks11
OP
Best Answer
Level 7

I've fixed this now, there was an issue with my scope

Please or to participate in this conversation.