Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marbobo's avatar
Level 12

Issues with New Resource Setup After Implementing Multi-Tenancy in FilamentPHP

I'm encountering an error while setting up a new resource for roles in FilamentPHP after implementing multi-tenancy. Here's the context:

I have two tables, 'users' and 'organizations', with a many-to-many relationship. The 'organizations' table serves as the tenant identifier in my multi-tenancy setup. Additionally, I have a 'roles' table related to users.

However, when attempting to create a new resource for the 'roles' table and navigating to the page localhost/app/tenant/roles, I receive the following error message:

The model [App\Models\Role] does not have a relationship named [organizations]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\Resources\RoleResource] resource class.

Could someone please guide me on how to resolve this error and correctly set up the new resource for roles in a multi-tenant environment with FilamentPHP? Any help or suggestions would be greatly appreciated. Thank you!

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like the error message is indicating that the Role model is expected to have a relationship with the organizations table, which is used to identify the tenant in your multi-tenancy setup. However, if your roles table does not directly relate to organizations, you'll need to specify the correct relationship that should be used for tenant identification.

Here's how you can resolve the issue:

  1. Ensure that your Role model has the correct relationship method that references the tenant. If your users belong to organizations and roles belong to users, you might have an indirect relationship through the User model.

  2. In your RoleResource class, you need to specify the correct relationship name that should be used for tenant scoping. You can do this by setting the $tenantOwnershipRelationshipName static property to the name of the method that defines the relationship to the tenant model.

Here's an example of what your Role model might look like with an indirect relationship to the Organization model through the User model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    // ...

    public function users()
    {
        return $this->belongsToMany(User::class);
    }

    // Define an indirect relationship to organizations through the User model
    public function organizations()
    {
        return $this->hasManyThrough(Organization::class, User::class);
    }

    // ...
}

And in your RoleResource class, you would set the $tenantOwnershipRelationshipName property:

namespace App\Filament\Resources;

use Filament\Resources\Resource;
use Filament\Resources\Forms\Form;
use Filament\Resources\Tables\Table;
use App\Models\Role;

class RoleResource extends Resource
{
    // ...

    // Specify the correct relationship name for tenant scoping
    public static $tenantOwnershipRelationshipName = 'organizations';

    // ...
}

Make sure to replace 'organizations' with the actual method name that defines the relationship from Role to Organization if it's different.

If you have a more complex multi-tenancy setup or if the roles are not directly related to the tenant, you might need to customize the tenant scoping logic further. In that case, you can refer to the FilamentPHP documentation or seek support from the community for more advanced configurations.

Remember to run any necessary migrations and update your models and resources accordingly to reflect the correct relationships and multi-tenancy logic.

marbobo's avatar
Level 12

@LaryAI tried this.. so here is my setup..

user has many to many relation to organization with pivot table

(users -> organization_user <- organizations)

roles has many to many relation to users with pivot table

(roles -> role_user <- users)

KhanZain's avatar

To resolve the error you're encountering with the new resource setup for roles in a multi-tenant environment with FilamentPHP, you need to define the relationship between the 'roles' table and the 'organizations' table. Here's how you can do it:

  1. Define the Relationship in the Role Model: In your Role model, make sure you have defined the relationship with the organizations table. Since you haven't provided your model code, I'll assume a standard many-to-many relationship setup.

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Role extends Model
    {
        public function organizations()
        {
            return $this->belongsToMany(Organization::class);
        }
    }
    
  2. Specify the Relationship in Filament Configuration: In your Filament configuration file (usually located at config/filament.php), specify the relationship between the 'roles' table and the 'organizations' table using the tenant() method. You can do this globally for all resources or per-resource.

    For example, if you want to specify it globally, you can add this to the configuration file:

    'resources' => [
        'role' => [
            'model' => App\Models\Role::class,
            'ownershipRelationship' => 'organizations',
        ],
    ],
    

    If you prefer to specify it per-resource, you can set the $tenantOwnershipRelationshipName static property in your RoleResource class:

    namespace App\Filament\Resources;
    
    use Filament\Resources\Resource;
    
    class RoleResource extends Resource
    {
        public static $model = 'App\Models\Role';
    
        public static $title = 'Role';
    
        public static $tenantOwnershipRelationshipName = 'organizations';
    }
    
  3. Ensure Proper Configuration: Double-check your configuration to ensure there are no typos or inconsistencies in naming. Make sure the 'organizations' table and its relationship with roles are correctly defined and accessible.

  4. Clear Configuration Cache: If you have a configuration cache, clear it to ensure that your changes take effect.

  5. Test: After making these changes, test your setup again by navigating to the role resource page (e.g., localhost/app/tenant/roles) to see if the error persists.

By following these steps, you should be able to resolve the error and correctly set up the new resource for roles in a multi-tenant environment with FilamentPHP.

1 like
marbobo's avatar
Level 12

@KhanZain thank you for the response. but Role dont have direct relation to organization organization.

user has many to many relation to organization with pivot table (users -> organization_user <- organizations)

roles has many to many relation to users with pivot table (roles -> role_user <- users)

Please or to participate in this conversation.