thiagomeloo / tenant
A package to manage multi-tenant applications in Laravel
dev-main
2023-10-27 02:29 UTC
Requires
- php: ^8.1
- illuminate/console: ^10.0
- illuminate/database: ^10.0
- illuminate/events: ^10.0
- illuminate/http: ^10.0
- illuminate/queue: ^10.0
- illuminate/support: ^10.0
Requires (Dev)
- laravel/pint: ^1.13
- pestphp/pest: ^2.19
README
Install
-
Install package
composer require thiagomeloo/tenant
-
Run migrations for creating tables tenants and domains
php artisan migrate
-
Create folder migrations tenant in database/migrations/tenant/app
mkdir -p ./database/migrations/tenant/app
-
Publish
-
Config
php artisan vendor:publish --tag=tenant-config --force
-
Route File
php artisan vendor:publish --tag=tenant-routes --force
-
Usage
-
Create tenant and domain
<?php #create tenant and domain $tenant = new Thiagomeloo\Tenant\Models\Tenant(); $tenant->save(); $tenant->domains()->create(['domain' => 'test.localhost']);
-
Create migration for tenant
#database/migrations/tenant/app <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('products'); } };
-
Create Model for Tenant
# app/Models/Product.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; }
-
Create Route for Tenant
#routes/tenants/web.php use Illuminate\Support\Facades\Route; Route::get('example', function () { dd("Ok"); }); #output: ok Route::get('save-product-example', function(){ Product::create(['name' => 'Product 1']); dd("Ok"); }); #output: ok