YouTubevideo· Aug 23, 2024· 31:04· 25K views on YouTube

Building a Multi-Tenant (Multi-DB) Project in Laravel with Turso

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

72 moments
  1. [Music] all right all right I hear you let's do it so this is the application that we're going to be building together and this is what I would call at least in its current state and you can find the of repo in the video description below it's a single database multi-tenant application what do I mean by that if I was to log in usually within any application that you buil for your users

  2. your users can then add resources to their account their team and while the definition of multi-tenant can be maybe multiple users per resources in this case maybe I have a team and users under that team well a team in a single database would be a multi-tenant application but for this purpose we're going to talk about what this looks like

  3. as is and then we're going to make it suitable for a multi-tenant multi- database application so in its current state I can create a new site let's call this the Lille daily when I create a new site that has its own subdomain Associated to it and multi-tenant doesn't necessarily just mean subdomains but it most of the time does mean that where your users can maybe attach a

  4. custom domain or maybe they just have subdomains and in this case what if we said that this is its own tenant well it is because we could manage posts for that subdomain where we could say let's just go ahead and call this I want to tell the whole world and maybe we can generate a paragraph here okay now when we visit this subdomain you can see here

  5. the larel daily. discuss. test so this would be a multi-tenant application just in a very small form being able to turn this application in this case our discuss. test application where we can create new site and manage posts for that site what if we just took all of that and put it into its own domain its own tenant so I could have an account on

  6. the Josh siri. disgust. test and there could be a ll. disgust. test that I could have a separate account or my data wouldn't even intermingle between those accounts but what are the different re versions of multi-tenancy let's take a quick look so there are two different ways to do multi-tenant applications there would be like the single database that we talked about and this could be a wide variety of ranges within this but

  7. it's mostly scoped queries where you could say okay give me the posts for the site and I'm actually doing that in my application here so if I was to say um on the web route I'm getting the site where the subdomain is equal to the subdomain and then I'm getting the posts for that site this is multi-tenancy there's multiple sites but I'm just

  8. doing it within a scoped query but then you could also have scoped queries where you have that tenance where you can say I have a whole tenant or a team or an organization and each organization has users Associated to that organization so you could technically have two user accounts and they don't necessarily have to be unique again it's all single database options but that would be like scope queries and then what we're going

  9. to transition this application into is a scope data multi- database application where instead of scoping the query we don't necessarily have to worry about it we can just say give me all of the sites on or give me all the posts for this site or give me all the sites because we don't necessarily have to worry about is that site associated with a subdomain it's just all in one database and in

  10. that case you could have multiple users or each tenant each account each organization could be different you could have different schemas this video is sponsored by turo terso is sqlite for production and we know as laravel developers and if you're not a Lille developer and you randomly stumbled upon this video uh laravel loves sqlite in fact it's the default whenever you create a new Lille project so I thought terso would be the best fit to show how

  11. can we use turo within a multi-tenant application and then if you want to take your learnings a little bit further down the road how can we expand the application we're going to build and make it even better with the offerings that turo has to give plus you can use the coupon code Josh sir for 10% off that's all one word so practice spelling my name j o h c i r r e we won't be able to get into all of the fantastic and wonderful things that turo does

  12. phenomenally well in this video but you'll get some tips on how to take your learnings to the next level in the sense of what could you build with terso with some of the features that they give but I'm glad that tzo sponsoring this video so we can get a little taste into what does it look like to have sqlite for scale the package that we're going to be using today to get us up and running as quickly as possible for this application to take it from that single database structure to a multi- database

  13. multi-tenant application is tendency for lirell this will be in the description below as well so let's walk through the steps of how to install this so that way we don't necessarily have to have scoped queries within our application we can just have multiple different versions of our application depending on who creates a subdomain anyone can log into that new subdomain or we'll call it tenant from here on out and then they could create their own sites and posts on those

  14. individual sites so let's get started the quick start in the docs is perfect way to get started with tendency for Lille we're going to be going pretty quick in this video and I'm not going to show myself coding through every single part of it but if you're following along this is what we'll do we'll just follow these installation steps and and then we'll get going so composer require and install that run PHP Artisan tency install then we can run the migrations

  15. that had scaffolded for us register the service provider in bootstrap providers. PHP and then we can create a new tenant model and since we want all of our models all of our schema and migrations in one of each one of those tenants we're not going to have too much in our Central database in our Central application we're just going to go ahead and name it tenant so I'm going just go and to grab this PHP artisan make model tenant and then in the tency config we

  16. just need to make sure that the tenant model is the tenant model that we created so app models tenant now tency for larel is going to handle all of the stuff that you don't want to set up yourself in the CA in the sense that you don't necessarily want to have to write all the boiler plate to say okay when someone visits a subdomain I want to store all that cash and switch the database and do all that stuff tendency for L is going to do it all for us and

  17. we just have to configure our route so in this case we have a central database or a central tenant and then we have our multi- database and so what we're going to do is just say we need this for all of our routes that are in our Central database so I'm just going to go to our web routes folder I'm going to paste this in and now I'm just going to think of what actually needs to be accessible here and that's really just going to be

  18. the slash page and that's pretty much it let's add the domain we're using to the central domains so let discuss. test and then we need our tenant routes and we're just going to wrap everything else in this so on our tenant routes we have this right here but since we're not using a initializing tendency by domain we're going to use the subdomain class

  19. that's been given to us and then I want to just take everything else and put it into this group right here and we also want to grab the off routes as well the tendency config is where we're going to be making any changes that we want the tency package to use a couple things I want to make a note of here is the prefix is going to automatically create a new database for US based on the

  20. specific manager that we're using so I'm just going to go ahead and hide the MySQL postres one so that we can only use SQL light this is our Central connection whatever we have placed in our DB connection and then we could set a template tenant connection if we wanted to this will be helpful down the road when we set this up with turo so setting this up within live 3 we need to add the configuration key middleware

  21. group to the tency provider and we can do that tency service provider and I believe this needs to go into the boot function yes bring in these classes and we'll use the subdomain I'm going to go ahead and run PHP Artisan migrate fresh just to get everything restarted but there's a couple things that I want to make note of first so in our database we have

  22. these migrations and now we have this new tenant folder we basically want any migrations that we want to run on newly created databases in this tenant folder posts sites jobs cash ideally we probably want most of it if not all of those in there so I'm just going to go ahead and move it and in fact we probably just want we also want the jobs

  23. folder maybe in the migrations as well just in case same thing with the cache so that way we have a cache in the central database as well as the tenant database and now since we don't have a users table or we'll actually move the users table to the tenant since we don't have a users table in the central database we have to create a new session

  24. table for our Central database so we can do that with PHP Artisan session Table and there we go now we have a cash jobs tenants domains and sessions let's take a look at the tenants we have our tenant and we have our domain and when we add a new subdomain when we create a new tenant then it just uses that as the subdomain again this is a lot of magic

  25. that's happening under the hood if you wanted to take a look and see how all this is happening or try to build it yourself this is a great package to look at okay now if I was the refreshes you see we don't see any like login or register buttons that's because in our route for our welcome page I have this does the route have a login and it doesn't because we don't have any users table anymore so what do we do why don't

  26. here we make a way to create a new tenant so that way we have this subdomain where we can add new sites so why don't I do it like this why don't we remove all of this and here we'll just make a live wire maybe create tenant volt components we can say PHP artisan make volt create tenant and then here in

  27. creates tenant what do we want to do we want to use the tenant model and the great thing about this multi-tenancy package is if we want to create a new tenant going back to the quick start we just call it like we would a model we could run tenant create and then we just add a domain to that tenant and then that's it we automatically have everything kickstarted for us now how does that

  28. happen in the tency service provider you see a little bit of that it's run by these things called events where we have a creating tenant and then the tenant is created we can create the database and migrate the database and you can customize these and hook into them and do whatever you would like to do within all of these events it just bundles it up for you behind the scenes and this is what we could do later on when we create

  29. a turo database we can use Tso's API to when a tenant is created create a new turo database and make those connections and configuration strings happen behind the scenes so going back to our create tenant component this is where we need a form to have a subdomain to create our new discuss site and that will create a whole new tenant a new database new migrations everything Associated to that

  30. subdomain so I'm going to do this behind the scenes and there we go in our create tenant function which is what we're submitting this form with all we're doing is creating the tenant and then we're creating a domain in this case a subdomain but the tency package adds the subdomains and domains in the same place and then we're just redirecting to that app in this case the new tenant that we

  31. just created now in a real world scenario especially if we were spinning up a new database when this happens in this case that we're going to you know hook into an API to create a new database within terso you probably would want this to be done in the background and then maybe you will send an email when the new tenant the new discuss site has been created has been instantiated but this will be good enough for now so

  32. let's go ahead and try it out enter your subdomain we'll say laravel and let's take a look we have ll. discuss. test SL register let's see what's happening so going into our tenant routes let's first because we don't necessarily need this anymore let's go ahead and get rid of that and what I'm going to do is actually I'm going to just grab all of the routes in the oth

  33. route and I'm we can go ahead and delete this and we'll go ahead head and move them into the tenants that way they're all in this one middleware route group so we can bring in the classes verify email controller there we go so I had accidentally dragged the tency service provider into the migrations tenant folder along with everything else so if

  34. we try this out we say laravel create your discuss and ah I think this is because there's still one extra step that we forgot to do within Live Wire so yeah setting up Universal routes within the tency for laral package ensures that the Live Wire requests can actually happen so we need to uncomment the tency

  35. do features config Universal routes so if we go to teny config and in features we should see Universal routes we need to uncomment that and then then in our middleware group we need to add the universal so that's going to be in tendency or in the app.php bootstrap middleware group Universal and now we

  36. should be good to go let's go ahead and clear the cache just to be sure larl and there we go so now look what happened when we did that if you see over here on the right you see this tenant a new database was created and what we can do then is we treat that as its own tenant so now it's automatically that tency package is automatically associating all the

  37. information to that database and that database alone so if I was to turn this on you see here level. discuss. test Josh we have some redirects happening and that is because in our environment another thing that I figured out within the tendency package is we need to turn this to session d driver file and that should be enough to do it let's see if

  38. we log in there we go okay it's probably because there are a bunch of different if we do session driver database it doesn't know which you know session to pull it from because we do need sessions enabled we probably don't but we it it's nice to have them enabled for our Central database or whatever application you're using most of the time you probably have more things going on in the central

  39. database for Live Wire sake we've turn the session driver to file and then we can create a new site the daily LL and create a new site and aha now let's go ahead and change this so there's a instead of a subdomain for site let's go ahead and change it to a path instead I'm going to do here is use a site subdomain and then

  40. in our site management for the site pass in the site variable do something similar right here there we go so now we have the daily LEL we can view the site there's no posts but we can go ahead and manage posts and let's go ahead and do the same thing for the Post where right here instead of the subdomain we can go ahead and just say this instead so we

  41. have the passing the site of site subdomain and again that's the tenant route that we created here where we have the site subdomain and then instead of passing it as the subdomain of a subdomain now that we have tenants we have the ability to have this lay. discuss. test/ sitethe daily Layel and now we have at least locally we have a multi-tenant multi-

  42. database structure where if I was to create a new post of this is so much fun and let's go ahead and generate a paragraph for that when we're viewing this site we see all the information again we didn't have to change too much of our code to make this happen but now we have discuss sites

  43. that we can easily create a new user for so inste we go to discuss. test let's do another one we'll just say Josh Siri now I have a Josh Siri application a whole organization a whole tenant where I can create new sites I was if I was to go we could probably make this a little bit better for this page here there we go so now you have the ability to log

  44. in and I could have someone create a new user in my tenant this is Josh siri. disgust. test so that's a rather simple way of getting a an application that we had in a single database with a scoped structure now we have a multi- database multi-tenant structure so how can we add

  45. turo on top of this and one thing I should know as well is that there's other packages within the larel ecosystem that you can do multi-tenancy with so larel multi-tenancy by spoty is a great package as well I opted for the tency one just because they make subdomains as well as that database creation and that event structure behind uh new tenants being created so easy they have a great page that compares it

  46. to other packages this spoty one is great if you just want something rather simple to scope something maybe even a single database to have that scope structure but within a single database So within turo one of the things that I love first off turo even though they're sponsoring this video I'm a huge fan of turo one of the things that I love even just on their starter the free plan is

  47. you get 500 databases for free now think of this if you were doing a multi- database multi- database application like this one you get 500 users for free and they each have their own database now why would you want to have multi- databases there's three reasons for it I would say one is that security was scoped so you don't necessarily have to worry about am I scoping this properly

  48. two you have the security of scalability where a lot of people within things like sock 2 if you're running a a business who was looking into that most of the time having multi-tenant databases just makes things easier down the line and then three is any kind of backup or just scalability vertically as well as horizontally where you only have to worry about um is if something goes down

  49. if that database for example is out of commission it's only it's mean to say but it's only one user or one tenant I should say all of your infrastructure structure is not at risk just because one database whether that was hacked or it failed in some way shape or form maybe didn't have a backup that you needed that's a great way to think about why multi-end databases but the starter kit starter plan is going to be

  50. incredibly fantastic for this where we can create databases programmatically so let's go ahead and check at the documentation well there's two things about tersa that I absolutely love that we're not going to dive in complet completely today but I wanted to take a look at as well one of them is embedded replicas this is where and even within the turo larel package you automatically

  51. get this out of the box with the PHP lib SQL driver but embedded replicas for a lack of better term it replicates what you have stored in turo in the cloud but it replicates that on your own server as well locally wherever you're hosting your application you get your SQL 8 file and they're synced so any changes that are made locally are then pushed to the

  52. cloud into where your turo database is set and why would you have turo well you can have databases on the edge you can have them all over the world but you can also have them embedded into your application as well so they also say Mak it more flexible suitable for various environments VMS vpss mobile applications as well one of the other things I love is the the multi-b schemas this feature is currently in beta but

  53. here's why I love this and what we can experiment with as well you can create a database in this CA case a parent schema database and this is going to be perfect uh for us within this application we have our tenant we have our tenant database migrations well instead of having to create a new database within the API and then hook up into it and then migrate everything thing and then

  54. maybe something is out of date the great thing within multi- database schemas is you create a database that is used as the parent schema database and then you can create one more databases that are used as the child databases and then we can apply schema changes to all of those that's just mind-blowing to me because what we could then do is instead of having to worry about migrations within

  55. this tency package we could just have one database that we connect to migrate all these things and then any new database we replicate that database we say this is a child of that database and then if we make changes to the parent database says right here it's automatically applied with that new schema and we can do this all within the API so we can say I want to be a part of

  56. this group group is important because we could have one API token to access all of those databases when we're doing this multi- database application and then the schema we just want it to be Associated to one database so let's get turo hooked up and then we'll take a look real briefly about some of these things that we could do and give you some tips on how to you know keep going keep learning

  57. keep creating so first off the driver for laravel you'll have to install the lib SQL extension for PHP and that link is right here and instructions or you can use the turo p P install especially if you are using something like laravel sale for example within laravel herd I think that this extension might be coming out soon but you can just follow these instructions and it's relatively

  58. simple within herd you can install additional PHP extensions like this and that's what I did as well I added it to my PHP ini file let's go ahead and install the package register the provider and then we have a local database connection so this is going to be using the lib SQL connection or this lib SQL driver is going to be using the sync URL to basically connect to turo

  59. grab your off token from turo and then your database URL and you can find that on the turo dashboard I've already created a uh database for this purpose I have a discuss Central database that's where we're going to be connecting locally and then we have our tenant database which is going to be the parent

  60. schema so you can go ahead and grab the URL and then create a database token here then we need to add this lib SQL connection to the database.php connections array so that can be just done right here so now if I was to run PHP Artisan migr fresh this is actually connecting to the turo database you can see here it's sing a little bit longer because we're synced we're connected so

  61. now we have everything in memory within our sqlite connection here but it's also synced to our turo database here's some things I wanted to take it just a little bit further just in case you wanted to do some digging on your own so the first thing is what if we use the API to create these databases when we clicked create your discust the first step is making sure that your tenant within this

  62. tenant package can connect to the lib SQL driver and you see here that we don't necessarily have a lib SQL database manager now this would be a great idea to figure this out I just created a brief mockup for this so if I was to turn this into lib SQL lib SQL database.php and now here's the

  63. interesting Parts is that if we were to take a look at the let's say the SQL light database manager for example it has the ability and the functions to create a database delete a database does a database exist and then the connection config so same thing with things like the postgres one you would just emulate this for lib SQL we already have the

  64. driver to connect to all of this because that's the turo laravel driver and so even with things like delete database make connection config I did a little bit of that with this lib SQL database manager where the connection config we can set that to the URL that we're setting in the database in this case in

  65. the domain database we can set the URL how do we do that in the tenants table this would be the perfect spot for a string of maybe the turo DB UR L and that way we can set this and use this when we change into our connection string upon tenant and again this database manager is doing all the hard work for us when it comes to finding the

  66. subdomain this is just telling us how to connect the Li and this isn't necessarily working we're diving in a fast bace I'm just saying this is what I would do if I was taking this one step further and then in the tency service provider this is where all of those jobs run when we need to create a new database or migrate a database for example we could probably comment those out because we can use that database migration schema that turo provides for

  67. us so what we could do here is make a new job we could say PHP artisan make job create turo database and then here we could say create turo database and again this is just in theory and of course this is incredibly simple to do within the API but within this create turo database job which we have automatically set up within this

  68. pipeline we could just say hey let's go ahead and create a new child database we just need an API token we could set it to a specific group and then we could set the schema in this case I already created a discuss Das tenant database that would be the schema that it's associated with and this just makes it a whole lot easier because after that's all

  69. created then this is why we have that lib seal database manager we can set the connection to say okay anytime we hit a new subdomain within our application laravel or Josh seror whatever that might be we can tell the turo database driver to switch that connection to the connection that we need now you can see here out the possibilities are endless not just within multi MTI tenant

  70. applications but within the things that turo gives you that just make things a whole lot easier whether that be multi- datase schemas or whether that just be the attached database function this allows you to have the ability to say okay I have a database but in my central database I also have information that I need how am I going to do that there are two separate databases attach database there's so

  71. many things that once you start thinking oh that would be really cool in this application oh I have an idea for that I could use in this application I think turo gives you that ability but hopefully this video gave you a little bit of ideas as well of how to take maybe a single scope single database application and turn it into multiton whether you're doing that on the edge with turo or whether you're doing that just for fun you can always keep learning you can always keep building

  72. you can find new ways to experiment with the things that you build so keep learning keep creating

just you here