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

Posted 3 months 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

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

/** @test */
public function a_student_can_access_student_pages()

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

    //assign them as a student

    //Sign them in

    Assert the student home page loads correctly

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.