Content Source:
How to develop multi tenant application using Laravel
If you want to run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups then it is pretty easily possible with this multi-tenant Laravel package.
With this package, you can serve multiple websites each with one or more hostnames from the same codebase. It has clear separation of database, assets & it also provides the ability to override logic per tenant.
This is suitable for marketing companies or startups which are building Software as a Service(SaaS).
I know this architecture is opposite to famous micro-service architecture, but just posting it in case someone is in the need to do so.
This package offers following features under the hood:
- Integration with the awesome Laravel framework.
- Event driven, extensible architecture.
- Close – optional – integration into the web server.
- The ability to add tenant specific configs, code, routes etc.
Database separation methods:
- One system database and separated tenant databases (default).
- Table prefixed in the system database.
- Or manually, the way you want, by listening to an event.
Requirements, recommended environment:
- Latest stable and LTS Laravel versions.
- PHP 7+.
- Apache or Nginx.
- MySQL, MariaDB or PostgreSQL.
Installation
composer require hyn/multi-tenant
Automatic service registration
Using auto discovery, the tenancy package will be auto detected by Laravel automatically.
Manual service registration
In case you want to disable webserver integration or prefer manual integration, set the dont-discover
in your application composer.json, like so:
{ "extra": { "laravel": { "dont-discover": [ "hyn/multi-tenant" ] } } }
If you disable auto-discovery you are able to configure the providers by yourself.
Register the service provider in your config/app.php:
'providers' => [ // [..] // Hyn multi tenancy. Hyn\Tenancy\Providers\TenancyProvider::class, // Hyn multi tenancy webserver integration. Hyn\Tenancy\Providers\WebserverProvider::class, ],
Deploy configuration
First publish the configuration and migration files so you can modify it to your needs:
php artisan vendor:publish --tag tenancy
Open the config/tenancy.php
and config/webserver.php
file and modify to your needs.
Make sure your system connection has been configured in database.php
. In case you didn’t override the system connection name the default
connection is used.
Now run:
php artisan migrate --database=system
This will run the required system database migrations.
Testing
Run tests using:
vendor/bin/phpunit
If using MySQL, use:
LIMIT_UUID_LENGTH_32=1 vendor/bin/phpunit
Please be warned running tests will reset your current application completely, dropping tenant and system databases and removing the tenancy.json file inside the Laravel directory.
For more Information and to build a website using Laravel, Hire Laravel Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft“.To develop the custom website using Laravel, please visit our technology page
- fullstackworld.com/
- github.com/tenancy/multi-tenant