Deep Dive into Multi-Tenancy: Laravel Filament v3 Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to my channel and today in this video I'm going to talk about multi-tenancy setup using filament3 yes but filament 3 has come out with its latest rather filament came out with its latest version and in this version the package supports multi-tenancy at its core in filament 2 it was possible to get stuff but you had to write a lot of code but in this version the multi-tenancy part of things is baked in and hence it becomes quite easy to set up stuff now do take care of the fact that this package comes with an opinionated way about how you can use multi-tenancy and I would say if you are building a SAS kind of an application where you have users who belong to teams and you know you have the tenancy based on teams this package does help you with this current setup now if you go to the get started section of this package and um get started you will see here there's a whole guide on how we can set up multi-tenancy now as I said there are multiple ways to handle multi-tenancy it's a big topic there are ways where you know multi-tenancy can be achieved with a single database there are ways where multi-tenancy can be achieved through multiple databases which means each tenant will have their own database these kind of setup makes sense when you have a lot of legal stuff in place if you are if your application is HIPAA compliant or stuff like that then you know those kind of setup does make sense the nutshell is there are quite a few things and there are quite a few opinions about multi-tenancy and we will see how this particular things comes into picture you should also take care of the security aspect while writing a multi-tenant application in a multi-tenant application specially when you are using a single database you need to ensure that all your queries and all your models are tenant aware which basically means that let's say you know in this example we will be creating a small CRM so my tenant customer should not be visible to your tenants customer list because if your user started seeing the customers from my tenant or my company it's a big problem right so these are certain things which although filament does handle for you know the overall setup but there are places where you can get wrong and hence it is important that you keep these things in mind and when you're developing your application you are constantly looking for those issues and you are ensuring that they don't come up in your application also you can write tests for those kind of stuff so that every change that you do ensures that you know those basic stuff are not breaking I guess we spoke about quite a few things even at the start of the video which is around what is multi-tenancy and the security stuff but yeah I wanted to ensure that you are aware of it before we even Deep dive and now that we are aware of it let's get started the first thing that we will do is obviously create a blank level installation so I'll go to my terminal maybe I'll do something like multi-tenancy or multi-tenant now I'm using heard for my Mac setup so that will mean that I will have a folder called multi-tenant.test on my browser so let's see if that is the case yes I do so we have our basic level application in place and now let's see what do we need to do to get filament up and running I'll open up my vs code so that we are able to browse through the code now those of you who feel that white is not the best theme maybe I'll just get something in place okay I like Cobalt as well all right so we have our code base now I'll walk you through the guide because the video is fine you will still need the documentation of um in a filament threes so that you know you can even do a deep dive on other sections so it is important that you you know understand where where I'm coming from now this initial piece is not about multi-tenancy but rather how you can set up your level installation with filament and there are few caveats which you need to understand the first one is if you see obviously for filament you need PHP 8.1 now I'm using 8.2 so that's fine learnable the latest version 10.1 Live Wire it will install automatically so that's fine and Ladder will live wire is in beta filament is also right now like I am on 15th August and this is not a stable release so you need to make certain tweaks the first one is over here it says minimum stability should be Dev in our composer.json because there is no stable release okay and that's the reason we need to do it but maybe in a while you are viewing this video if things have stabilized then you don't need to change it but right now we do okay I have this as Dev now the next thing that we need to do is run this composer require filament filament 3.0 stable okay let's just do that all right so I have installed the package and now if we go to our documentation the next step is installing the panels of filament now there is one thing which you need to understand in filament 2 there were different packages and there was a possibility that you could install them as an independent ones for example you could use the table component or the form component as an independent stuff now in filament 3 as well there are multiple stuff for example we have panel Builder we have form Builder table notifications accents and many other stuff but if we install panels it would install everything else I have seen there yes you know using other filament packages so it says that the filament panel Builder pre-installs the form Builder table Builder notifications actions so on and so forth so this is something which you need to care uh take care when you are doing your stuff okay now uh what does it say all done would you like to show some love I have already done that so right now I'll skip this all right and then I need to create a user because without the user how will be login now in here what I'm trying to do is Maybe do an sqlite kind of a thing so database dot SQL light go to my EnV o sqlite as well and then run PHP artisan migrate GRT okay since the tables are created and now I can create the user so there's a command which filament provides us it will ask for a name email address the password and we are done if I now go to admin slash login which is the typical login page for a filament application let's try that I'll hit sign in and we are in so everything is properly working but this is the basic package right we haven't done anything as of now and this is where we will start implementing multi-tenancy okay so I'll go to multi-tenancy and there are Concepts which you need to read upon but one important thing is you know previously in filament two we had a config file which was controlled controlling a lot of behaviors and three and I understand this is from the you know I would say Paradigm where even laravel 10 is headed towards which is you know a lot of configurations are now happening inside the service providers and the config files are reducing significantly now so this is the initial panel again a side note I will cover uh in details about you know these things in my filament three course but in filament I mean the main container is a panel okay you have the ability to create panels for different users with different roles and stuff like that and whenever you create a new um user right and you create the first filament stuff there's one panel which is automatically created for you and we will do a lot of configurations inside this panel itself so yes this is where a lot of our configurations will go so what is the first thing that we need to do so for tenancy to work in filament three at the core we need to Define one entity or one model which defines how the tenancy is created by that what I mean is see a tenancy can be based on a team a company organization whatever you call it but the basic thing is that that entity defines the ownership of the data with the overall application so let's just say I am in a team which is called amitab Roy right so any entity and in our CRM example any customer that I create right the ownership of that customer will be defined by this particular team that I am in and that's why this entity is very important so we will create an entity very similar to what the example here shows which is a team so PHP artisan make model now I'll show you the beautiful thing which comes with the latest version which is where the new improved prompts it's such a beautiful you know CLI based Improvement um I really like it so team you know it says is ask for the name well I I put it in and then can you see this nice looking you know the multi select kind of stuff it's just so interesting way of you know interacting with the framework now what do we need we need do we need a database seeder as of now no we don't I would say I just need the migration okay I'll just be happy with that um let's go to the database migrations and teams so I have two columns for my database which is the name and the slug the slug I want because I'll show you that in the URLs there is you know a way to identify a tenant and it shouldn't be ID so this is where the slug will come into picture now one word of caution right now filament three is only supporting uh one user um I would say belonging to multiple teams okay you can't have a one-to-one mapping if you are creating that kind of stuff you can stop it through a logical level inside your code base but the package in itself right now expects a belongs to many kind of relation between the users and and the model which is kind of showing the ownership and in our case that's the teams right so I need one more table which is the mapping stable so team underscore user and I added two more columns one is the foreign ID for team which will translate into team ID underscore ID and the foreign ID for user which will translate into user underscore ID and both of them are indexed this is how you know we have the pivot table where the information will be stored we will need to make a few changes to our model as well so inside the team we have our name and slug being fillable properties and then we Define a few relationships for example a user belongs to many team okay so I have this in a relation in place where I'm defining that a user belongs to many teams similarly in here I'll have users and this will be also belongs to manipulation now if we do that if we refresh nothing happens why because so far we haven't told filament that and there is some you know tenancy in place so as the document suggested the first thing that we need to do is we need to Define that there is a tenant and the Tenant is defined by this model so let's do that I save it I go to my application and I hit refresh and it says there is some undefined method how did it came in we need to do a deep dive so let's see let's go to the documentation first so it says that you will also need to tell filament which tenants a user belong to you can do this by implementing the has tenants interface so now if you're aware in filament 2 as well there were two traits or rather one important trade which we need to implement especially when the application needs to go into production mode which is filament user and the second trait is hastenance now we are not in production as of now so I will not implement it but do take care of that and I will cover this in my in a full course but has tenant is something which we definitely need to cover so let's go to our user model and over here I'll do implements has tenant now immediately it will complain that I'm not I haven't declared those methods what are the two methods that we need from here one is obviously this and the second one is get tenants we need we will change a little bit of the code because um so obviously the first thing is can access tenant this basically shows the logic it's an important thing where in a filament will decide whether the current user is able to access anything from a particular tenant or not which basically means let's say I am right now in only one tenant which is tenant id1 and what will happen is if I change the url okay because the tenants are identified through the slug so if I change the url and I make it let's say two or three right will I be able to view the list of data for that particular tenant ideally no and this is the function which will take care of that so how do we do that we Define this rule by saying that can access tenant has this function okay let's push that in and understand what is happening first of all so this teams which is a belongs to many relation so in that we are checking whether the current tenant is being contained or not which basically means if I have two or three or four tenants in which I am involved this function will check that is still complaining and I wonder why so I'll have model in place and that's gone fair enough similarly in here collection collection eliminate database eliminate support I think it should be eliminate support if I'm not wrong by the way I'm not returning any collection so that's where get tenants how do we get tenants so let's see what they have done obviously it is just returning the relationship that's fine we just do this teams and it is still complaining method get tenants is not compatible with that um the reason is what does it return um looks correct I think the panel is not imported Maybe yeah the panel was not imported and hence it was complaining so now we have made certain configurations now let's quickly read up what it says in the example user belongs to many teams so there is a team's relationship that get tenants method Returns the teams that the user belongs to which is fine you know we saw that another get tenants method for security you also need to implement the can access tenant method right and it is as I said it is the main function which determines whether the current user has access or not now if I refresh no such table as teams which is fine let me migrate okay and now if I refresh it shows a 404 why because now there is no concept of a user belonging to a tenant we just created the user right we haven't added any relationship or any data so that filament understands that the current user is in the current tenant if I even do this which is you know ideally that will happen you know the tendency is taken through the slug it doesn't work so how do we achieve that now one way is obviously if I do tinker and um let's just create a tenant so a little app models team create is there a problem no um sorry name is focal works slug something like that okay and go over here we'll do team equals this and I can now do team um members let me see was it members or was it users I think it was users yes users attach use I'll do app models user find one okay and if I go over here right so I have my team underscore user I have that relationship so ideally now if I refresh voila it worked because now I can see this thing which is my um current tenant and I am able to log in and I can look at my dashboard so basically we have covered the first step where a user logs in and just do a sign out once more and I'll just sign in so right now because the user has only one tenant it is directly taking the user to the database sorry the dashboard and it shows that you know this is the tenant which is selected but now things have not completed there are a lot of things which we need to still cover and the next step is basically how do we ensure that if a user just registers it is supposed to create a tenant for himself or a team for himself so that you know he can log in so let's see so for the tenant creation there is already a good example if you go to the documentation you will see adding a tenant registration page so how do we add this into our application well I'll first create this register team page which is basically a filament-based page okay I'll um inside filament folder this so I'll just copy that piece and over here new file like so right so we have filament Pages tenancy registration register team I'll open the PHP tag that's the bare minimum and then we can literally copy paste most of the stuff in here because we are following the documentation so you know a lot of things are taken care of you don't need to worry um the namespace is missing I haven't copied it I think all right so what do we have in here we have a register team which extends the registered tenant thing it's a simple page right we have a label for that we have a form in the form I'll add one more thing which is the slug so that you know we understand how the form works but it's a typical form well you know it's like any other Live Wire sorry filament based form if you have used ever you will be uh you will know that okay do we have the team model we don't so what I will do is I need to get that in here okay so what are we doing over here so this is a form and the handle registration is a function which will be called when the form is submitted in here we first create the team and then in the team members we attach the current logged in user which is quite straightforward so let's see how that translates um a log out I don't think I have the ability to delete records from here do I no so maybe what I can do is create a new user okay app models right so we have the basic thing if I do a create the user got created in our database and ideally I should be able to login so let's hit the sign in button and see what do we get hmm it didn't work and why that would be the case let me try it again admin login okay it is coming back and I think I know what the problem is I didn't follow the entire documentation because right now our application doesn't know that there is a register team page and this is what will um you know tell filament that this is a registration page so I'll go into my register team no in here tenant registration so we'll call that method tenant registration and register team is the page that we need to pass over here so let's see if I hit refresh do admin slash login once more okay it already takes me to the register team page why because this user is not attached to any tenant so I'll create my new tenant John team now we can obviously automate the slug I don't really uh want the end user to do it but then yeah that's how it is now it is complaining again and that call to undefined method team members and let's understand why so I have my user which has teams but the team okay the team also has a user so where where did we go wrong um I have team members okay this needs to be members and not users so or I can change that to member members whatever makes sense will it work again let me see the database what happened um I have teams only one team and one entry so my understanding is John company or John Doe uh John team should work again register team and this time I am presented with that particular thing and can you see here is one more drop down where it allows me to register one more team so let me try and do that John team we had a John company now this one is John team and right now John Doe has access to two companies right this is how you know we get the ability for a user to create a tenant if that user is not attached to a particular tenant and then we have we have the ability let's say you know to have a registration page where you know we already know which tenant the user will be registered to so we can you know make those necessary changes but the filament way is this where you know you are able to attach a tenant to a user when he or she is registering right now there is also one more thing which is you know the changing of the profile or tweaking the thing I'll show you that as well adding a tenant profile page so we need to create a edit profile editing profile page inside here oops new file so over here and then I'll just copy this entire thing so I have my name space I need to include the model this will be slug okay there's no need for the team so I've edit form blah blah blah okay and right inside panel I need to Define it so tenant profile and editing profile let's do that tenant profile edit team profile let me see profile right edit is this the correct one yes so I'll do that and if I now go to my application edit profile can view doesn't exist I think I have done something wrong edit team profile right this is anyways a wrong thing I'll just remove that and come over here again edit team profile let's see yeah this is loading and if you see we have John team so I can do team profile edit we can change stuff here and we can do John company team profile and change it here as well some other team save changes go to the dashboard and I can see those changes in here right so this is how the tenant registration and the edit of attendance details can be possible in filament and if you see so far we haven't done any major development we are only configuring things most of the configurations are inside the you know this file which is admin panel provider because typically in filament a lot of things gets done using the configuration and the same concept is in V3 as well now apart from this niceties there is a whole section about billing okay so filament three does integrate well with spark so if you have a spark plan you should be able to configure billing you know the renewable subscriptions and stuff like that and there is a whole lot of documentation around that now unfortunately I haven't used spark in any of my project as of now most of the projects where I have worked in um because they are Indian we have Razer pay and some custom providers as well but yes so far I haven't used spark so I won't be able to give you any general overview of that however I did see an interesting thing which is writing a custom billing integration which basically means that if I want to have a razor pay implementation and all those things are being available with Razer pay then I can write an integration for filament three okay so this is possible again but I haven't done it I haven't used spark as well so I won't be able to tell you much about that you can explode it on your own all right now in our application we will introduce a new entity which is customers because we said that the example will be a CRM so when we add an entity called customer we would want that if I am an intenant a then I would should see only the customers from tenant a and when I am in tenant B I should obviously see all the customers from tenant B and no other customers should be visible and this is the basic requirement for any multi-tenant application which is all or entities should be tenant aware and that is the most important aspect which is how is the package allowing us to make the entities tenant away so let's see there is a section where we can see how we can configure the tenant relationship and it says when creating and listing records associated with the tenant filament needs access to two eloquent relationships for each resource an ownership relationship that is defined on the resource model in our case that will be the customer relationship because the entity or the eloquent model will be customer and a relationship on the tenant model class in our case it will be the team class right so how do we add this let's see the first thing that I will do again is artisan make model sorry okay and the name of the model will be customer I just need the migration that's done now let's look at I'll just close everything maybe I don't need the user admin provider is fine okay I'll just delete these two file okay um all right so create what is this customer right so I'll add only one field um as of now which is name not going into the entire details of doing all the names and phone numbers and stuff and I'll add one more basic field which is foreign ID for and in here we'll have team because then it will translate to team ID and that is how the relationship will be defined which is a customer belongs to a team all right so I have my team class over here the first thing we will do is customers and we will Define this being a has many relationships so a team has many customer okay um yeah let's define the relation or sorry return type and so this is the second part of it I basically clicked on and you know moved to that part which is the relationship on the tenant model but we still need to define the relationship on the main entity which is customer so first of all protected fillable name that's the bare minimum and then we will have a relationship called company or rather team sorry not company and this is a belongs to team okay add this now we have the basic model in I mean the model in place we have some relationships we still don't have the filament resource file we need to create that but let's understand what we are doing we have this basic thing which is team has customers customer belongs to a team but then if you go into the documentation we also need to tell what is the ownership relationship okay and this is important so we'll go to our admin panel provider tenant so this one is model okay I need the I know these attributes so that's the first one and then ownership relationship ownership relationship is team okay I defined this as well now we will add a resource for customer so PHP artisan make filament resource customer and I should have a new resource file inside customer resource correct let me do a PHP Artisan oops Artisan migrate created the customer table let's see database sqlite yes I have this this is empty as of now obviously um so we need to create the form text column name okay now bear with me if you are new to filament obviously this is not the video because I'm assuming you have some basic knowledge of element 2 or filament three but I will definitely create a series on filament three and that time I will go into the details of why I'm adding these stuff but for now if you are moving along and if you are not clear bear with me and just go for the ride okay um so right we have name [Music] if I refresh I see now a link called customers and in here it says I don't have any customers I'm going to go into the John team again the same now if I create a new entry it says text column given table is fine text okay this is wrong text input make name now it should work yeah um so let's add a customer this is John first customer made an entry second customer if I go to the list okay it updated it sorry John first customer so in here for tenant John team I have two customers now if I switch some other team go to the customer I don't see those customers if I add one more some other customer one other customer can you see these two are different customers compared to the John team customer list right and if we go into the database as well in the database I can see all the four customers but in here you will see that we have the proper team ID and if I show you the resource code can you see I haven't done anything additional for the um how to say the team ID to get populated right I am not saving the team ID as the current user's team ID or the current tenant right I'm not doing that this is all taken care by the multi-tenancy of filament V3 and that is where you know I would say that it is doing enough out of the box for you to get started but yes there are definitely a lot of things that you need to take care of so we can see now that you know the tenancy is allowing us to you know create resources which are tenant aware we can do a lot of stuff there's only one thing which is kind of you know haunting me which is this URL right now is using the ID in this Slug and this is um showstopper we can't go live with an application where the tenant ID is sitting there in the URL it does and allow the user to get a sense of the you know the number of tenants and stuff like that and that is really not something which you know I would be comfortable so there are certain ways we can hide that which is I'll just go over here the slug attribute and now you would understand if you remember I had a field called slug in my team and that is what I'm going to use over here so let's configure it uh slug attribute right so I'll go over to my admin panel provider in here we will have a slug attribute and I will add the property which is Slug how is it defining that well my model name is steam so it understands that this is the model and this model has an attribute called Slug and so filament is now aware that instead of the ID the slug will need to be taken care of obviously now if I refresh it goes for a 404 and if I go to admin voila we have admin slash some other team and if I change it to John team the slug changes now if you also want small little tip again no big deal um there was a yeah tenant route prefix this is something which I really liked um you know it gives you the sense that you know the package owner thinks a lot about these small small things so I can provide a prefix a route prefix to you know all the URLs so if I do teen as my route prefix now if I refresh can you see now it says admin slash team slash the name of the team so these are small stuff with the package which I really liked about but overall the multi-tenancy part is just amazing it allows me to get started with any idea that I have about a SAS platform it has the ability to you know integrate with the overall application make your entry entities or the resources tenant aware um you know it adds those uh you know Fields so that you don't need to take care about it and also the fact that it has an integration with spark and it allows you to write your own billing um provider it's definitely a very strong competitor when it comes to creating your SAS application or your next big idea using filament V3 so I am pretty excited about this and I had a few side projects where I am planning to use this let me know what you think about this and and how are you going to use the filament v3s multi-tenancy in your application yeah that's about it guys that's what I wanted to cover in this video If you like this video then do click on the thumbs up icon don't forget to subscribe to my channel
Info
Channel: Amitav Roy
Views: 11,756
Rating: undefined out of 5
Keywords: filament v3, filament, laravel, laravel filament, multi-tenancy, filament multi tenancy, saas app with filament
Id: cKgx4T3AUWY
Channel Id: undefined
Length: 46min 9sec (2769 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.