16 Laravel Filament Multi-Tenancy - FilamentPHP V3 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends Tony here now in this video I want to talk about the multi-tenancy with a filament so I'm inside the filament documentation in the multi-tenancy section so here multi-tenancy is a concept where a single instance of an application serves multiple customers so each customer has their own data and access rules that prevent them from viewing or modifying each other's data this is a common pattern in SAS applications users often belong to a group groups of users often called themes or organizations and the records are owned by the group so by a theme and users can be members of multiple groups so multiple teams for example now multi-tenancy is a very sensitive topic it's important to understand the security implications of a multi-tenancy and all the properly implemented if implemented partially or incorrectly data belonging to own tenant may be exposed to another tenant now filament provides set of tools to help you implement multi-tenancy in your application but it is up to you to understand how to use them the term multi-tenancy is Broad and made mean different things in different contexts filaments tenancy system implies that the user belongs to many tenants for example teams and may switch between them if your case is simple and you don't need a many-to-many relationship then you don't need to set up the tenancy in filament you could use observers or Global Scopes let's scroll down and let's see the setting up tenancy the setup of tenancy you will need to specify the tenant like theme or organization model in the configuration file so we need to change the tenant method and pass in here the modded in our case is a good to use the department as a team but I think is better to use team because you are going to understand better if you see my video and also coming here and read the documentation which I suggest you so also watch my video and also come here and see the documentation and you are going to have Team here and also team in the video and that is going to be better for you to quickly understand that okay so first let's go and create the theme model I'm going to open the terminal and let's say here artisan make the model hit enter and let's name it theme and we want only the migration okay and let's open that migration so themes migration and here I'm going to add the last time table string for the name and let's copy also and paste for the slab let's save let's come here you also need the tel filament which tenants a user belongs to so you can do this by implementing the S tenants interface on the user model but first is call on the configuration file and change the tenant method pass in the theme modded let's come here and let's open the admin panel provider and at the end I'm going to chain tenant method pass in the model which is theme class okay save then let's open the user and implementing the has tenants interface so let's do that let's open the user model and here say implements has tenants okay and we need to create the get tenants method themes relationship and can access tenant method okay let's copy them and scroll down and paste it right here so we need to import the panel from the filament panel the collection from the illuminate support collection belongs to many and also the model here now we have got tenants we have teams which is a belongs to many relationship and for that we need a pivot table user okay but let's save this and let's come here and let's say in this example users belong to many teams so there is a team's relationship the get tenants method Returns the themes that the users belongs to and filament uses this the list to the tenants that the user has access to so it's going to list all the themes and then for security you also need to implement the can access tenant method well this has tenants interface to prevent users from accessing the data of other tenants by questing there ID okay for that we have this scan access Tanner now let's create also the pivot table and I'm going to do that in this one so let's copy this and paste it down there this is going to be team user origin ID for team ID and say constraint and let's copy and paste for the user ID save this also we need to say here drop is exist also the team user save now adding a tenant registration page so a registration page will allow user to create a new tenant when visiting your app after logging in users will be redirected to this page if they don't already have a tenant when we register a new user this is not going to be long immediately to the tenant so for that it's going to be redirect to the register page the setup or register page you will need to create a new page class that extends a register tenant this is a full page Live Wire component and you can put this anywhere you want such as inside the app filament Pages tenancy register thing size suggestions add inside the pages tenancy and register team let's copy this path here and I'm going to create that file inside the filament let's say new file paste that path hit enter and let's pass the PHP first and then I'm going to copy what we have in these examples okay copy this and paste it right here so we have a get label method a form and to this form you may add any form components you want song with that text input for the name and also the text input for the slack because we have also the slack on the T okay and now we need to tell filament to use this page and you can do this in the configuration file we need a chain tenant registration passing the registration theme page okay so let's open the provider let's change the tenant registration passing the page which is a register team class okay let's save this also customizing the tenant registration page so you can override any method you want on the base registration Base Class to make it art as you want also you can add a tenant profile page a profile page will allow users to edit information about the tenant and set up the profile page you need to create new page class that extends edit tenant profile this is a full page Library component the same as the register class and you can put this anywhere you want such as element Pages tenancy and let's copy this and open the filament Pages tenancy and here create a new file with this name edit theme profile PHP say PHP here and let's copy what we have inside here paste it and here we have a get labeled theme profile and also a form and let's do the same on the form let's add also the text input for the slab let's save and you may add any form components also here on the form method and now we need to tell filament to use this page the same we have done with register we need to change the tenant profile passing the page okay so let's open the provider and chain at tenant profile passing the page which is edit theme profile class okay so add this page here and let's say this also here we have customizing the tenant profile page you can override any method you want on the base profile based class to make it a as you want and it's we have also building here but let's navigate on the configuring of the tenant relationship when creating and listening records associated with a tenant filament need access to eloquent relationships for each resource an ownership relationship that is defined on the resource model class and a relationship on the tenant model class by default filament will attempt to guess the name of this relationship based on standard learnable conventions for example if the tenant model is team it will look for a team relationship on the resource model class now we are going to use employees so employees is going to be the resource model class and it's going to look for a team relationship on the resource model class and let's do that let's open the model employee and create this relation public function theme and this is going to be belong belongs to so here let's return this belongs to team class but for that we need the modify also the database so let's go to the migrations and we need because of the employee is stable and here let's copy this and duplicate then change this theme ID save because now the employee is going to belong to the team also we need so let's open back we need to modify the name of the themes so the teams is going to be up here above the employees for that I'm going to change just the date to be zero four and now teams is up here let's close like this his attendant model is theme it will look a theme for a team relation on the resource multi-class okay and if the resource model class is post it will look for a post relationship on the tenant model class on the theme model is going to look for post but in this case in our case it's going to look for the employees so let's go to the team model and here first let's create the protected dollar sign fillable to be equal with the name and the slack okay then let's create the employee's relationship so public function employees employees and this is going to be as many relationship and let's return don't assign this has many employee class okay that also if I open the register team here we have this handled registration and let's import the theme we had here 2017 team create the data but because here we are going to handle the data and then call in the team members but these members we don't have members or relationship so let's go and create that on the theme method okay so we need to create this method and let's copy and say here public function members which is going to be belong to many relationship and let's return this belongs to many user class save okay now customizing the ownership relationship name so you can customize the name of the ownership used across all the resources at once using the ownership relationship argument on the tenant configuration method and here is the example let's copy this and let's go on the panel provider on the tenant method Now list pass this one ownership relationship which is going to be team not the owner on the employee we have Team here okay alternatively you can set the dollar sign tenant ownership relationship name static property on the resource class but I'm going to use this one customizing The Reef resource relationship name also you can do that and also here we have for the slack but let's go and let's see so far what we have if I refresh let's run the migrations and let's refresh again now because we are logged in here we have we are directed to the register team okay so we redirected here already 13. with a two inputs name in the slug and here is the name and the slack so let's register a theme and I'm going to register a party awesome name of the team and it's party as a slug let's register the team and we already directed back here we have now the theme spotty and we have also the theme profile because we created this edit theme profile and that's Navigator so we can update the theme let's say spotty theme for example and the slack to be spotty theme like this save changes refresh now we have spotted theme Here we can register a new team so let's go there and let's say for example Lara casts and also here I'm going to say Lara cast like this register team and yeah we have laracas and spotted as you can see when we we're in Lara cast and we are admin Slash 2 so the ID of the team if I change the spotty we have slash lock here okay now let's see configuring the slug attribute and for that we need to say on the tenant method slug attribute to be slack let's copy this and let's navigate on the provider on the tenant method now let's add also to override the slack and if I save come here now and it's called the dashboard for for not found because it's not going to found a slug one but if I refresh here we navigate the spotty Dash team the slug belongs to this themes and if I navigate Lara cast admin slash now we have here okay now let's navigate to the employees and create new employee we can see here column not found on now column employees theme ID and we added so let's see let's open again the database migration we added the team ID here but I think we need to disclose all of them now and let's open the terminal and let's uh I run the Artisan migrate Flash and I'm going to say that as seed okay now let's just navigate the slash admin refresh the migration and we redirect admin new so it's going to register theme so let's register again it's party and the slack is going to be spotty resistor team we are here the spotty team here okay let's go to the employees and let's create a new employee song with the say big pillar okay yeah we don't have also the department because the country stayed in City we have generated with so let's go back let's come here let's navigate on the Cedars database Seether so we created the country state and city and let's create two departments here right here I'm going to create two departments and let's say Department or just one create and we need to add only the name we sent a b okay and let's run again The Artisan might get press type test seed refresh navigate back to the admin spotty spot the legislative team go to the employees new employee fake Fielder select learnable here and also the date let's create imv have a typo here employees not employees so let's open the theme and say employee here save and now let's say create and employee created let's see if let's go to the employees and yeah it is now let's go and create a new thing register a team and I would say Lara class again her register team okay we are inside the larakas team if I go to the employees we don't see employees here so let's create a new employee now for the Lara casts theme okay let's say fake filler fill all the fields let's create and let's navigate now back we are on direct us team we have this employee here with this name if I change the theme to spotty and navigate the employees now we have this one this employee here okay now as you can see here we have two and we can we need the fix that also if I go to the country the country does not have a relationship name team so we need also to have a relationship and a counter is going to beat the belong to a team but for now we just I wanted to show you on the employees I created this video because I had a comments on YouTube how to do that and for that I'm going to do this one now if you like such a video don't forget to subscribe to my channel like the video share with your friends and see you in the next one all the best
Info
Channel: Code With Tony
Views: 11,072
Rating: undefined out of 5
Keywords: laravel, vuejs, livewire, laravel 8, laravel 9, laravel crud, laravel 8 tutorial, laravel 9 tutorial, filamentPHP v3, filamentPHP, laravel 10, filament tutorial, Laravel filament, laravel filament php, laravel filamentPHP tutorial, Filament v3 tutorial, master Laravel filamentPHP, learn, learn Laravel Filament admin panel, laravel admin panel, Laravel models and migrations, laravel multi-tenancy
Id: ZLjV-CSSK0U
Channel Id: undefined
Length: 23min 53sec (1433 seconds)
Published: Fri Sep 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.