In my app I have a many-to-many relationship between users and organisations,
User
public function organisations()
{
return $this->belongsToMany('App\Organisation')->withPivot(['owner']);
}
Organisation
public function users()
{
return $this->belongsToMany('App\User')->withPivot(['owner']);
}
There is a pivot column on this relationship to state whether the user is an owner of the organisation or not.
I am struggling to test the relationship though, my test returns,
Failed asserting that null matches expected 1. Expected :1 Actual :null
Here is my test function,
public function testUserHasOwnershipOfOrganisations()
{
$userRow = factory(User::class)->create();
$organisation = factory(Organisation::class)->create();
$userRow->organisations()->attach($organisation->id, ['owner' => true]);
$user = User::first();
$ownOrganisations = $user->organisations()->wherePivot('owner', true);
$this->assertEquals($organisation->id, $ownOrganisations->first()->organisation_id);
}
The thought process of the above is make a user, make an organisation, create the relationship. Query the first user in the table, get their organisations and make sure the organisation in the organisation relationship as the same ID as the one I just created.
Can anyone tell me why this is not working as I would expect it too?
Thanks