Skip to content

hasTeamRole() throws error when team_user.role is null (but schema allows null) #1585

@mattwells-coex

Description

@mattwells-coex

Jetstream Version

5.1.1

Jetstream Stack

Livewire

Laravel Version

12.34.0

PHP Version

8.4.13

Database Driver & Version

No response

Description

Calling hasTeamRole($team, $role) on the HasTeams trait can result in a runtime error when the team_user.role column is null.

The Jetstream migration allows the role column to be nullable

The method passes the value directly into Jetstream::findRole which does not allow null.

Steps To Reproduce

<?php

use App\Models\Team;
use App\Models\User;
use Laravel\Jetstream\Tests\Fixtures\User as UserFixture;
use Laravel\Jetstream\Tests\OrchestraTestCase;

class HasTeamRoleTest extends OrchestraTestCase
{
    public function test_hasTeamRole_returns_false_when_member_pivot_role_is_null(): void
    {
        $team = Team::factory()->create();
        $user = UserFixture::find(User::factory()->create()->id);
        $user->teams()->attach($team, ['role' => null]);

        $this->assertFalse($user->hasTeamRole($team, 'admin'));
    }
}

Related Pull Request

See #1584 for a proposed fix. This issue documents the underlying bug for clarity.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions