Support the ongoing development of Laravel.io →
posted 1 year ago
Artisan
0
moderator

Hello @juanchos2018

I think you mean multi tenant, is that correct? Or do you have an explicit package in mind?

For a multi tenant application you need to decide if you want to have all data in the same database or a database for each tenant. For both options I don't see any reason that it can't be done by SQL Server.

0

uhmmmm, but I have tried with sqsrv however it has not worked, since its managers only have : 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class, 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,

cap-marin liked this reply

1

I have the same problem with sql server, if I found a solution, I will write here...

0

If you want to adopt Stancl\Tenancy, you may create your own SqlServerDatabaseManager class to add in tenancy configuration:

// config/tenancy.php
'managers' => [
    'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class,
    'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class,
    'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
    'sqlsrv' => App\Services\SqlServerDatabaseManager::class,

Obviously, you must install Microsoft drivers for PHP in your server...
0

I have created my class for SQL Server. If you want me to share it, please email me at: [email protected]

0

Would you share it here? It can be useful for other devs (if it doesn't contain sensitive data).

0

Of course, here is the code for the class I created. You should first configure the database.php and tenancy.php files to establish the connection with SQL Server & call this class in managers:

'sqlsrv' => App\Services\SQLServerTenantDatabaseManager::class,

<?php

declare(strict_types=1);

namespace Stancl\Tenancy\TenantDatabaseManagers;

use Illuminate\Database\Connection;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\NoConnectionSetException;

class SQLServerTenantDatabaseManager implements TenantDatabaseManager
{
    /** @var string */
    protected $connection;

    protected function database(): Connection
    {
        if ($this->connection === null) {
            throw new NoConnectionSetException(static::class);
        }

        return DB::connection($this->connection);
    }

    public function setConnection(string $connection): void
    {
        $this->connection = $connection;
    }

    public function createDatabase(TenantWithDatabase $tenant): bool
    {
        return $this->database()->statement("CREATE DATABASE \"{$tenant->database()->getName()}\" ");
    }

    public function deleteDatabase(TenantWithDatabase $tenant): bool
    {
        return $this->database()->statement("DROP DATABASE \"{$tenant->database()->getName()}\"");
    }

    
    public function databaseExists(string $name): bool
    {
        return (bool) $this->database()->select("SELECT * FROM sysdatabases WHERE name = '$name'");
    }

    public function makeConnectionConfig(array $baseConfig, string $databaseName): array
    {
        $baseConfig['database'] = $databaseName;

        return $baseConfig;
    }
}

I hope it's helpful. :3

Last updated 10 months ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.