[Live-Coding] Laravel Multi-Tenancy with Single Trait

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is populace from laravel daily and in today's video we will create a demo project with multi-tenancy lateral what is multi-tenancy I have an example for you we have an admin panel a management system for hotels and bookings and let's imagine we have two agents John and Brian or John and Sally whatever and each of them should manage their own hotels and their own bookings so currently we have two bookings by John and two two hotels by John and two hotels by Brian so what if inside of the organization John works with Hilton Hotels and Brian works with Radisson hotels or vice versa and same with bookings so one would add booking and only the person who added the booking actually sees that and the other agent doesn't see so that is kind of a multi-tenancy which means basically every user sees only their own entries and not others you have examples like lateral Forge or any actually any web site with a huge domain huge database but you have only your own part your own mini database with your stuff and that is multi-tenancy so currently I have a project generated actually that's the first public project generated with which admin part on new version 2019 I will put that on github so you can see that code generated but it is not multi-tenant so it's just a crud for hotels and crud for bookings which is empty for now that is my local machine and now we will make it multi tenant without quick admin panel with just coding with observers so there are a few ways to do that multi tenant thing and I will show you the straightforward way with observers and global scopes in eloquent and then we will make it better flexible with traits so we will reuse it for bookings let's begin so for now hotels don't have any field for who created that Hotel in the database if we see the database it looks like this so first what we need to do is create it by user ID we add that field and we do the migration so HP artisan make migration ad created by user ID to hotels table okay we are in phpstorm and we need to add that field into our migration so unsigned integer created by user ID unknowable because we do have records already so in order to migration not to fail we make them knowable now but we will change that later and then we add foreign key so foreign on the same field references references ID on table users which is default lateral table users okay and then we need to add that field as a fillable in the model so hot on model and fillable we add create by user ID and then let's create a relationship as well an elephant just in case maybe we will need it in the future so belongs to user class with field created by user ID okay and we launched the migration here with your PHP artisan migrate successfully and we should have new field quit by user ID okay that's step one now we need to make that field automatically fill them with new hotel so for that we create an observer PHP artisan make observer Hotel observer that's one way of doing the multi-tenancy I repeat first is with observers and then I will show you how to do that with traits more flexibly so we add an observer it should generate the file observers hot observer and we're interested in created thing so created Hotel created by user ID equals of ID and let's check if off actually exists just for just in case and what they'll say if I think like that next step is to register our observer an app service provider service provider I think it's the syntax is this from whatever number hotel observe and then Hotel observer class and now let's try it out in the web we go to our hotels form let's try form filler just random stuff submit we have our hotel and let's see if that actually is filled yeah we have quickbuy user ID equals two so now the first step is done we are actually observing the new hotels and assigning the current user ID for the hotels the next step is to filter the data and to filter by user we return to the model hotel and we need to add both methods so public function boot every model has a boat metal boot method and first one you should called parent boot and then we can do whatever next thing is static ad global scope and we will add global scope with name created Hotel created user for example an odd function query with return Larry we're mmm no it's not query function I think it's builder builder eloquent return builder we're created by a user ID was off I did again let's check that if off share then we'll return it ok final thing here is this function should be static I almost forgot and now this should filter any query by hotels by user ID so let's refresh our page with hotels list so currently we have five hotels but only one of them is with our user let's refresh and we should see only one as you can see so now even if I try to launch hotels one for example I would get probably 404 yep 404 so I don't have access to any hotel which I haven't launched myself I haven't edited myself so but if I change that to create by user ID to that for example this is my hotel then I do access that probably yep it is working so in the list there should be two entries now so let's assign radisson stew to user ID 2 and hilton's to user ID 3 which is different so now we have filtered the entries and the observer works now what I wanted to show you later is make it more flexible so now we need to add it to bookings so booking should be also filterable by user so if we do it the same way as hotels we do create by user ID then we do the observer and then we do boot method in the model so three things right what if we add a trade that it were it would work for any amount of models and this is exactly what we will do so we will add a trait AB let's make a folder called trait traits and then less out of trades like filter by or create actually what's called multi-tenant turbo and that will make different models that we want not accountable so trade multi-tenant able and then we add a net method called boot boot mode talent able it will actually serve as kind of a constructor to that trait if you will and then instead of doing this we will do the same thing in a trait so actually we will copy static add global scope so that's on on query static at global scope and let's make it more flexible rename the scope name to be quick by user ID for any model and also we need to add builder to the to the use section or actually use I think this will be more elegant use builder and then builder here okay that should be good and then also we can add the same thing on static created or in fact creating functional creating function model this is a little different syntax than observer but it will do the same thing so wait by user ID well equal or ranging and let's wrap that all with if of check to avoid misunderstandings of check then we do both thing so we copied both things this is from observer and this is from model boot global scope let's delete them both from here so we don't need boot model anymore and we don't need global scope in observer so app service provider we don't need that observer anymore because it will be served by a trait and those two are not needed what we need to do is to add multi talents about trade to our hotel model so we use multi terrible and then multi terminal and probably we need to add namespace name space trades a list of hotels trade multi-talent able not found of course because it should be multi tenant able with app trades now has a namespace let's launch that again forward static code Boden water tenant a book of course it should be static public static next and we have our three hotels so it works let's try to add number four and let's see if creating actually works creating words we have four hotels out of six I think in a database so total we have six but only four of them are visible to us and now if we want to add multi-tenancy to bookings all we need to do is well create the field create created by ID next let's do it yeah so HP artisan what does it make migration ad created by user ID to bookings table so that is again the migration quiz to booking stable we will copy and paste the whole code here because it's the same so quit by user ID also in booking model we add that to our philipose so button from created by user ID here and also let's user user eloquent relationship with created by user ID field and all we need to do now is in the booking we should add that trade so this model will be also motor turns about use motor turn on scible and here motor tenant alone and now let's try to add a booking and let's see if that actually we need to launch migration of course parties on autism migrate okay we add a field and then let's add something booking from time from date from today to Friday we submit actually notice the list of hotels it gives for it doesn't give six so even in drop-down so every query every eloquent query every builder query would actually launched that the same filter so we submit and we have our first booking but if we login as other user have that in Firefox let me open Firefox bookings that's another user it should be empty the list is empty because no data is available because it's not my booking my booking for a user is this one and in the database if we see bookings table that is created by user ID - so in this way I will recap now so we created a trade multi Toronto book which does both things that we did before with observer so we don't need observers anymore so this should be deleted as well we have a trade and if we want to make some crowd some model multi-talented boo we just added here and here and that's it so you can of course you need to add quit by user ID to all those models I hope this video was helpful and multi-tenancy is a much deeper subject so I think I will create more videos on that like how to create multi talented teams maybe we'll review some packages of multi-tenancy so see you in the next videos of youtube channel and please subscribe to be notified of the next videos
Info
Channel: Laravel Daily
Views: 27,000
Rating: undefined out of 5
Keywords: laravel, php
Id: nCiNqboYFVQ
Channel Id: undefined
Length: 16min 0sec (960 seconds)
Published: Mon Mar 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.