/simple-jetstream-multitenancy

Simple multi-tenancy for Laravel Jetstream apps

Primary LanguagePHPMIT LicenseMIT

Simple multi-tenancy for Laravel Jetstream apps

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Simple package to support multi-tenancy in Laravel Jetstream using a trait, with some development niceties thrown in.

Installation

You can install the package via composer:

composer require chrisdicarlo/simple-jetstream-multitenancy

And then run the install command. This will prompt you to specify which model is the tenant and publish the config file:

php artisan sjm:install

Usage

You can create a migration to add the appropriate tenant columns to tables that should be tenant aware:

php artisan sjm:migration App\\Models\\Post

This will generate a migration file that looks like this:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->unsignedBigInteger('tenant_id')->nullable();
            $table->foreign('tenant_id')->references('id')->on('users');
        });
    }

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropForeign('tenant_id');
            $table->dropColumn('tenant_id');
        });
    }
};

Lastly, add the ChrisDiCarlo\SimpleJetstreamMultitenancy\TenantAware trait to the applicable model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Chrisdicarlo\SimpleJetstreamMultitenancy\TenantAware;

class Post extends Model
{
    ...
    use TenantAware;
    ...
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.