Step-by-Step Guide to Filament Multi-Tenancy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is how you can set up multi-tenancy in your filament admin panel using five simple steps but first let's be clear on what multi-tenancy means and I think the best way to describe it is with an example if you have ever used Trello you know that you can have multiple workspaces and in those workspaces you have multiple boards and on those boards you have cards in this context your workspaces are your tenants and your boards and cards are the resources multi-tenancy means that no other user has access to your tenants and your resources so now that we are clear on what multi-tenancy means let's see how we can implement it in your admin panels what I have here is a simple filament admin panel with no resources and in the code we have three models one for labels one for tasks and one for teams and of course the default user model in the this example the theme model class is going to serve as our tenant and the label and task models will serve as our resources step number one will be to configure the tenant class in our panel configuration so let's open app providers filament admin panel provider and to configure a new tenant all we have to do is call the tenant method and here we pass the class name that we want to serve as a tenant so in this case team step number two is to configure all of our models and relationships let's start with the user model let's open app models user and in order for the user model to work with multi-tenancy you need to implement the has tenants contract as soon as you do this you're going to need to implement two methods first method is called can access tenant and the second one is get tenants let's start with the can access tenant method create a new public function called can access tenant and notice how we are receiving the current tenant as a parameter in this method so here we can just return these teams and this is a relationship that doesn't exist yet but we are going to create in just a minute and on this relationship we can check that it contains the current tenant so basically if the team or the tenant is contained on any of the teams this user has access to then we return true now let's create these themes relationship create a new public function called teams this will be a belongs to many relationship and then return this belongs to many team all that is left is to implement the get tenants method so create a new public function called get tenants and here we just return all of the themes or tenants this user have access to so let's say return these teams so that takes care of the user model now we need to work on our resource models which are task and label all of these models will require relationships so that they can work with filaments multi-tenancy let's start with the label model by default filament multi-tenancy needs two relationships the owner relationship which is defined on each of the resource models and the inverse of that relationship which is defined on the tenant class so in this label model we can Define the ownership relationship by creating a new public function called team this will be a belongs to relationship then we can just return this belongs to team class since we are going to need the same relationship in the task model let's just copy this method open the task model and paste it here and don't forget to import this relationship name and now we need the inverse of these two relationships which are defined in the tenant model so open the theme model and let's create a new public function called labels this will be a has many relationship and here we can return this has many label class now let's create one more relationship for the tasks this is also a has many relationship so let's return this has many task class and since we are here let's also create one more relationship or the team members again let's return this has many hi this is me from the future uh first of all PHP is not dead so you can relax and second of all that right there is a mistake because that's supposed to be a belongs to many relationships not a has many I'll come to realize this in a few minutes but if you want to fix it right away now is your chance okay bye user class filament will always try to guess the relationship names based on laravel's default name conventions but if for some reason you don't follow these conventions you can always customize your own relationship and the Tenant relationship as well to customize the owner relationship directly in your panel configuration you can open app providers filament admin panel provider and inside the tenant method you can use the ownership relationship parameter and here you can change the name of the relationship that you are using for example you can change this to owner and that would mean that all of your resource models will need the owner relationship another option is to customize this directly in your resource classes so for example let's create a resource for our labels in a word of caution when I was working on this video I discovered that if you use the simple lag to create a simple resource any records that you create will not be assigned to the current tenant I don't really know the reason but I already posted a question in the filament Discord server so as soon as I have an update I'll pin it in the comments section now you can open app filament label resource and then you can use the protected static string tenant ownership relationship name and change that to owner and once you do that again you have to go to your models and change the name of the relationship Additionally you can also customize the tenant relationship name using protected static string tenant relationship name [Music] and let's say for example that the relationship name is theme labels now you would have to go into your tenant model and change the relationship of that resource to team labels but in this example we are going to go with the default level convention step number three would be to create a tenant registration page so that every user that logs into the panel can have the ability to create a new tenant or a new team if the user has not been assigned to any team yet to achieve this all we need is to create a class that will represent the tenant registration page you can put this file anywhere you want but it's recommended that you put it inside filament and create a new folder called pages and within that folder create another folder called tenancy and in here we are going to create a new file called register team [Music] in the interest of time I'm just going to paste all of the code for this page and then walk you through it real quick as you can see this is a simple class that extends from the register tenant Base Class and from this class we overwrite three methods first is the get label method which simply returns a string that is the label for this page then we have the form method which simply defines the form schema or the page in this case we only need one text input for the team name and finally we have the handle registration method which receives the form data we use this form data to create a new theme and then we attach the currently logged in user to that team finally we just need to return the team like this now that we have the registration page class we just have to configure it for our panel so open app providers filament admin panel provider and then you just have to call tenant registration and here you pass the class name that we just created [Music] now when you try to log in with a user that has not been assigned to any team you will be automatically redirected to the team registration page and looks like I made a mistake and instead of using the belongs to many relationship on the tenant's relationship board members I use has many so let's correct that right away open the team model and here is where I made a mistake this should be belongs to many now if we try this again the user is immediately assigned to the tenant and we also have the ability to create more themes step number four will be to create a tenant profile page where the user can modify the tenant information in this case the team name this process is very similar to what we did when we created the tenant registration page all we need to do is create a new page and we are going to put it in app filament pages tenancy so let's create a new file and let's call it edit theme profile and again in the interest of time I'm just going to paste all of the code and walk you through it this is yet another class and we are extending from the edit tenant profile Base Class and from this class we only need to overwrite two methods the get label method again returning just the label for this page and then the form method where we Define the form schema to edit the tenant information in this case we only need one text input for the theme name with this class created all we have to do is add it to the panel configuration so again open app providers filament admin panel provider and below here we can call Tenant profile then pass the class name that we just created [Music] and now in the tenant drop down menu we have the ability to edit the team profile step number five would be to be aware that if you are using select inputs with relationships in your forms the options will not be scoped to the current tenant to demonstrate this first I'm going to configure the relationships between label and task open the task model and let's create a new public function called label this will be a belongs to relationship where this task belongs to a label and this is label okay much better now let's create the inverse of that relationship in the label model with public function tasks this is a has many relationship where a label can have many tasks [Music] now let's create a resource for the task model [Music] if we examine the task resource class first of all in the form we don't need the select or the team and we already have one select for the label using the relationship that we just created first I'm going to create a couple of labels for these tenant and looks like we also have a drop down for the theme so let's remove that open the label resource class and in the form let's remove this select from here so let's say that on the standard we have two labels started and finished and let's also remove the theme column from this table again in the label resource let's look for the theme name column and remove it now I'm going to create a new tenant or in this case a new team [Music] and then create a couple of labels for the team blue tenant let's call them assigned and then done [Music] if you move to the task resource and try to create a new task when you examine the label drop down you can see that you have the labels for all of the tenants and not just the current one you can solve this problem in two ways the first option will be to use the relationship method of the select input in here you can use the modify query using parameter and this will be a callback function that receives a query Builder and here you can limit the results of this query using where belongs to and then use the filament facade with the get tenant method this will only show Label records that belong to the current tenant and now the label options are correct the other way that you can solve this is by assigning a global scope to the model using a middleware class so let's create a new middleware class and let's call it apply tenant scopes you can find a new class in app http middleware [Music] in here you can use label add Global scope this takes a callback function that receives a query Builder and here we can do the same thing using query where belongs to filament get tenant now we need to register this middleware in our panel configuration so open app providers filament admin panel provider and look for the middleware array [Music] let's go back to the task resource class and let's remove this parameter if we examine the label options we can see that we achieve the same result that'll be all for this video if you found it helpful don't forget to click the like button subscribe to the channel and I'll see you in the next one if you are very
Info
Channel: Tuto1902
Views: 6,094
Rating: undefined out of 5
Keywords: laravel, filament, laravel tutorial, filament tutorial, laravel 10, filamentphp, filamentphp form
Id: Ak5taG5BtLY
Channel Id: undefined
Length: 17min 38sec (1058 seconds)
Published: Fri Sep 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.