PHP code example of jeffersonsimaogoncalves / multitenancy

1. Go to this page and download the library: Download jeffersonsimaogoncalves/multitenancy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeffersonsimaogoncalves / multitenancy example snippets


'providers' => [
    // ...
    JeffersonSimaoGoncalves\Multitenancy\MultitenancyServiceProvider::class,
];

use Spatie\Permission\Traits\HasRoles;
use JeffersonSimaoGoncalves\Multitenancy\Traits\HasTenants;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasTenants, HasRoles;

    // ...
}

Tenant::create([
    'name'    => 'An Identifying Name',
    'domain'  => 'tenant1'
]);
Tenant::create([
    'name'    => 'A Second Customer',
    'domain'  => 'tenant2'
]);

$user = User::first();
Tenant::first()->users()->save($user);

protected $routeMiddleware = [
    // ...
    'tenant.auth' => \JeffersonSimaoGoncalves\Multitenancy\Middleware\TenantMiddleware::class,
];

Route::group(['middleware' => ['tenant.auth']], function () {
    // ...
});

protected $routeMiddleware = [
    // ...
    'tenant.guest' => \JeffersonSimaoGoncalves\Multitenancy\Middleware\GuestTenantMiddleware::class,
];

Route::group(['middleware' => ['tenant.guest']], function () {
    // ...
});

use Illuminate\Database\Eloquent\Model;
use JeffersonSimaoGoncalves\Multitenancy\Traits\BelongsToTenant;

class Product extends Model
{
    use BelongsToTenant;

    // ...
}

app('multitenancy')->currentTenant();

Tenant::create([
    'name'      => 'Admin Portal',
    'domain'    => 'admin'
]);
bash
php artisan vendor:publish --provider="JeffersonSimaoGoncalves\Multitenancy\MultitenancyServiceProvider" --tag="config"
bash
php artisan multitenancy:install
bash
php artisan multitenancy:migration products
bash
php artisan multitenancy:super-admin [email protected]