In this article, we are going to discuss how we can setup multi-database in laravel filament admin panel.
What is multi-tenancy?
Multi-tenancy is one of the critical architectures in modern web applications, wherein a single instance of an application serves many clients or tenants without compromising on data isolation. Available on SaaS-based solutions, various organizations or users use the same application that shares the very base but have different databases or logically separated data.
Types of Multi-tenancy
Single Database:
In this type of multi tenancy, all tenants data are stored in a same database with a tenant identifier. By default, Filament Multi-tenancy supports this type of multi-tenancy structure.
Inventory Management System project is based on this structure. You can check out the video below.
Multiple Database
In this type of multi-tenancy, all tenants data are stored in their own (different) databases. They all are completely isolated and secured.
Installing Laravel and Filament
Lets create a new project using Laravel and install the filament package using composer.
laravel new multitenant-app
After that, once our project is ready, you can install filament on it . So i will just put the link of filament installation here. You can check the guide to install or also you can check above video tutorial to install the filament.
Read more about filament tips and tricks.
Install Stancl/tenancy
For multi-tenancy setup , we are going to use the stancl/tenancy package. I highly recommend you guys to go through the docs.
Let’s follow the steps below for setup.
Install Package
First of all, lets install the package to install multi-database in laravel filament. So for that use the following command on your terminal.
composer require stancl/tenancy
After installing the package, we need to run the command to install its scafold.
php artisan tenancy:install
This code will create some tenancy related setups, create migration files and so on.
Furthermore, lets migrate those migrations using the php artisan command.
php artisan migrate
Register the TenancyServiceProvider
You need to register the TenancyServiceProvider on config/app.php file.
'providers' => [
// ...
App\Providers\RouteServiceProvider::class,
App\Providers\TenancyServiceProvider::class,
// ...
],
Create a tenant model
Now we need to create a tenant model that will extend the base tenant model from the package and use 2 Concerns.
class Tenant extends BaseTenant implements TenantWithDatabase
{
use HasDatabase, HasDomains;
}
We need to implement the TenantWithDatabase contracts and then we have used those 2 concerns.
Register the new tenant model on config file
After you created a new tenant model, we need to change on config/tenancy.php file to make sure it uses the new tenant model.
'tenant_model' => \App\Models\Tenant::class
Routes File
Note: If you don not have RouteServiceProvider , then update the following in routes/web.php.
foreach (config('tenancy.central_domains') as $domain) {
Route::domain($domain)->group(function () {
Route::get('/', function () {
return view('welcome');
});
});
}
Similarly, we also need to modify our routes file. For that inside app/Providers/RouteServiceProvider.php file, we need to insert the following code:
protected function mapWebRoutes()
{
foreach ($this->centralDomains() as $domain) {
Route::middleware('web')
->domain($domain)
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
}
protected function mapApiRoutes()
{
foreach ($this->centralDomains() as $domain) {
Route::prefix('api')
->domain($domain)
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
protected function centralDomains(): array
{
return config('tenancy.central_domains');
}
And in the boot() method , we need to modify it to call out above methods.
public function boot()
{
// ...
$this->mapApiRoutes();
$this->mapWebRoutes();
// ...
}
Github
If you want a github link to the repository, please comment down below or email me with github username. I will add you as a collaborator.
I simply could not go away your website prior to suggesting that I really loved the
standard info a person supply on your guests? Is going
to be back incessantly in order to check out new posts
Because the admin of this website is working, no uncertainty very shortly it will be
famous, due to its quality contents.
Hola!, esta muy bueno.
Aguardo el código.
Saludos.
Sebastian.
Pingback: Implementing Tenant-Specific File Uploads in Laravel with stancl/tenancy: A Complete Guide - Code Daily
A brilliant solution – concise, efficient, and solves the issue perfectly.
I appreciate the clarity and thought behind this implementation.
Elegant and pragmatic solution. Handles the edge cases gracefully and keeps the codebase clean—hats off for this one.
Clear implementation, thanks for great efforts
Please i want the repository
Can you share me your github email? I didnot find with this.
Can you please share me the repo?
https://github.com/ssuvam-dev/multi-tenant-app.git