Member-only story
A full-featured multi-tenant app with Laravel Part 1 — Setup
Part 0, Part 1 👇, Part 2, Part3, Part4, Part 5, Part 6, Part 7
In this part we’ll accomplish the following tasks:
✅ Create a project
✅ Setup a system database and a user
✅ Install and configure multi-tenant package
✅ Add an artisan command to create a tenant
✅ Add an artisan command to delete a tenant
1. Create a new Laravel project
Let’s get started by actually creating a new Laravel project. This is easy:
$ laravel new townhouse
2. Set up a system database
Just like with most any other web apps, townhouse is going to need a database and a database user. But unlike most other web apps this database user is going to be a bit different. Because our app will create 1 database for each tenant, our database user needs special privileges to do so.
Note: The following step for creating a database and a user is only applicable to MacOS+SequelPro users but you should be able to perform these on your choice of OS and your choice of MySQL client in the similar fashion.
If you don’t have it already, download and install SequelPro. Connect as root to your local MySQL server and create a new database name townhousedb.
Create a new user, townhouseadmin, and set the password as secret. Go to Global Privileges tab and click Check All button. Go to Schema Privileges tab and select townhousedb from schema list. Select all Available Privileges by hitting CMD+A keyboard shortcut and click < button to grant all the selected privileges. Finally, click Apply.
Now, let’s make our app aware of this database connection. You may have always left the name of your default database connection just as it is — mysql — but because we’ll be using multiple databases, just calling it mysql may create some confusion in future. So, we’ll call it system instead.
Open config/database.php and change the name of the second connection from mysql to system.