I've fixed this now, there was an issue with my scope
Mar 4, 2019
1
Level 7
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?
Level 7
Please or to participate in this conversation.