#Get spatie/laravel-permissions user team id

5 messages · Page 1 of 1 (latest)

worldly coral
#

Tags show what other stuff I'm using

Currently:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $user = $request->user();
        dd($user->roles);
        /*Illuminate\Database\Eloquent\Collection {#1362 // app/Http/Controllers/UserController.php:16
            #items: []
            #escapeWhenCastingToString: false
        } */
        return $user;
    }
...

Although:

<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        $permission = Permission::create(['name' => 'Test']);
        setPermissionsTeamId(1);

        $role = Role::create(['name' => 'Test']);
        $role->givePermissionTo($permission);

        $user = User::factory()->create([
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'asdfasdf'
        ]);
        $user->assignRole($role);
        dd($user->roles);
    }
}

Shows the correct information:

Illuminate\Database\Eloquent\Collection^ {#341
  #items: array:1 [
    0 => Spatie\Permission\Models\Role^ {#1851
      ...
      #attributes: array:6 [
        "id" => 1
        "team_id" => 1
        "name" => "Test"
        "guard_name" => "web"
        "created_at" => "2025-02-10 09:26:13"
        "updated_at" => "2025-02-10 09:26:13"
      ]
...
worldly coral
#

From what I gathered:

you need to first setPermissionsTeamId wherever you're trying to access the $user->roles:

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        setPermissionsTeamId(1); // Valid team ID
        $user = $request->user();
        dd($user->roles);
        return $user;
    }

However, I'd plan to somehow save the team in a way and not have to hard-code the value in there. The obvious solution would be to add the field to the User model, but I had understood that laravel-permissions had relations for this using a pivot already

#

Effectively there is a model_has_roles table which maps this information correctly

worldly coral
#

I've got a seeder:

public function run(): void
    {
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        $permission = Permission::create(['name' => 'self']);

        $role = Role::create(['name' => 'Test', 'team_id' => 1]);
        $role->givePermissionTo($permission);
        setPermissionsTeamId($role->team_id);

        $user = User::factory()->create([
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'asdfasdf'
        ]);
        $user->assignRole($role);
    }

Does the assignRole($role) not do what you'd expect? why aren't the roles actually visible on the user model?

worldly coral
#

I just disabled teams in spatie's permission.php configuration file and now everything works as expected, or at least the functionality is more intuitive