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

Posted 1 month ago by matttonks11

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?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.