Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating Tenant Databases - Multi Database #27

Closed
wahyubaskara opened this issue May 29, 2020 · 38 comments
Closed

Migrating Tenant Databases - Multi Database #27

wahyubaskara opened this issue May 29, 2020 · 38 comments

Comments

@wahyubaskara
Copy link

wahyubaskara commented May 29, 2020

I've been follow all steps in the documentation, from creating landlord and tenant db connection then create a couple of rows in the tenants table with correct database name. After that i ran this command php artisan tenants:artisan migrate but the result always give me error: Invalid catalog name: 1046 No database selected

Full error message:
Screen Shot 2020-05-29 at 11 59 14

My database config:
Screen Shot 2020-05-29 at 12 00 45

My tenants table in landlord database:
Screen Shot 2020-05-29 at 12 02 44

My local databases:
Screen Shot 2020-05-29 at 12 03 53

My multitenancy.php config file:
carbon

@martincarvalho
Copy link

martincarvalho commented May 29, 2020

try updating:
'tenant_finder' => null,
to:
'tenant_finder' => Spatie\Multitenancy\TenantFinder\DomainTenantFinder::class,

@wahyubaskara
Copy link
Author

wahyubaskara commented May 29, 2020

Not working, same error occurred

@freekmurze
Copy link
Member

What is the output of config('multitenancy') in a tinker session?

@wahyubaskara
Copy link
Author

Sorry, how can i get the output?

@freekmurze
Copy link
Member

Run php artisan tinker

@wahyubaskara
Copy link
Author

Here is the result

Screen Shot 2020-05-29 at 23 44 43

@freekmurze
Copy link
Member

what is the output of config('database')?

@wahyubaskara
Copy link
Author

okay here is
carbon

@juanparati
Copy link

@wahyubaskara: try to change:

"default" => "mysql"

by

"default" => "tenant"

@wahyubaskara
Copy link
Author

Solved! You saved me, thank you👍

@kurucu
Copy link

kurucu commented Oct 4, 2020

@wahyubaskara: try to change:

"default" => "mysql"

by

"default" => "tenant"

Oh my word, this has been tripping me up! Is this in the docs?

@masterix21
Copy link
Collaborator

@kurucu, no, isn't in the docs because the default connection is a state of mind: depends on your project and your approach.

@Saimon-git
Copy link

hello spatie guys and @freekmurze
I am contacting you because I have a problem with the multitenacy package, I can not get the connection to the tenant to work, I have as default connection "tenant" this is the error that I throw

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

Screen Shot 2021-04-29 at 21 07 55

@masterix21
Copy link
Collaborator

@Saimon-git, have you followed the documentation?

@Saimon-git
Copy link

@Saimon-git, have you followed the documentation?

thanks @masterix21

@refaatgamal
Copy link

I have the same error but all above i made it and still this problem exist
bruno-kelzer-LvySG1hvuzI-unsplash

@masterix21
Copy link
Collaborator

@refaatgamal are you using the SwitchDatabaseTask?

@refaatgamal
Copy link

Yes and now all tenants works well but I can't make landlord working but tenants works

@masterix21
Copy link
Collaborator

@refaatgamal, I'm sure it's a misconfiguration issue. Please dig inside reading our documentation. Thanks.

@refaatgamal
Copy link

Already working as docs but landlord didn't work how can I login as landlord I make virtual domains in local host for tenants but when I go to project direct with out tenant domain it gives me no database selected issue

@arslanakram1
Copy link

@refaatgamal got any solution on this issue? I'm trying to implement the laravel multitenancy and facing the same issue that you mentioned in your last comment. Thanks

@masterix21
Copy link
Collaborator

Sorry, but I still think that is a coding problem. Please create a GitHub repository with your sample code that generates the errors: I'll try to help asap.

@Azmeer-crescent
Copy link

Azmeer-crescent commented Feb 23, 2022

I am having the exact same problem after doing this:
"default" => "tenant"

I can login to tenants but NOT to landlord account. getting the same error as:
refaatgamal commented on May 31, 2021

But when I switch the line back to:
'default' => env('DB_CONNECTION', 'mysql'),
Both landlord and tenants started working ! My DB_CONNECTION is pointing to the landlord database

@Noor600
Copy link

Noor600 commented Mar 28, 2022

I got this bug
Screenshot 2022-03-28 215157

@Noor600
Copy link

Noor600 commented Mar 28, 2022

`'default' => 'tenant',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'url' => env('DATABASE_URL'),
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

    'tenant' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => null,
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

    'landlord' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => 'landlord',
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],`

@Noor600
Copy link

Noor600 commented Mar 28, 2022

@masterix21 Is there a solution to this problem ?

@masterix21
Copy link
Collaborator

Please post your Admin model

@Noor600
Copy link

Noor600 commented Mar 29, 2022

class Admin extends Authenticatable {
	use Notifiable;

	protected $table = 'admins';
	protected $fillable = [
		'email',
		'name',
		'photo_profile',
		'password',
		'group_id',
		'remember_token',
	];

	protected $hidden = ['password'];

	public function group_id() {
		return $this->hasOne(\App\Models\AdminGroup::class, 'id', 'group_id');
	}

	public function role($name) {
		$exists_group_id = $this->getConnection()
			->getSchemaBuilder()
			->hasColumn($this->getTable(), 'group_id');
		if ($exists_group_id) {
			$explode_name = explode('_', $name);

			if (!empty($this->group_id()->first())) {
				$role = $this->group_id()->first()->role()->where('name', $explode_name[0])->first();
				if (!empty($role) && $role->{$explode_name[1]} == 'yes') {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}

@Noor600
Copy link

Noor600 commented Mar 29, 2022

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select * from settings order by id desc limit 1) (View: D:\delivery\resources\views\admin\login.blade.php)
I think this problem not related with Admin model

@Noor600
Copy link

Noor600 commented Mar 29, 2022

I created two databases and two domains
php artisan serve --port=8001

in two domain I got this problem

@masterix21
Copy link
Collaborator

Admin model is in the landlord or in the tenant database? Why you didn't prepare your model? Take a look here: https://spatie.be/docs/laravel-multitenancy/v2/installation/using-multiple-databases#content-preparing-models

All models in your project should either use the UsesLandlordConnection or UsesTenantConnection, depending on if the underlying table of the models lives in the landlord or tenant database.

@Noor600
Copy link

Noor600 commented Mar 30, 2022

Ok I Will prepare the models
thanks bro ❤️❤️❤️

@Noor600
Copy link

Noor600 commented Mar 31, 2022

@masterix21 unfortunately the same problem
:(
I need an urgent solution

@masterix21
Copy link
Collaborator

masterix21 commented Mar 31, 2022

The open-source package philosophy lies in free cooperation: if you are here to search for an urgent solution, the best way is to dig inside the code to find it yourself.

Anyway, please create a public GitHub repository with your code to help me to understand where your errors are.

Thanks.

@Noor600
Copy link

Noor600 commented Mar 31, 2022

ok
thanks 3>

@Noor600
Copy link

Noor600 commented Mar 31, 2022

@masterix21 I created new repo and I invited you

@abdallah0128318
Copy link

abdallah0128318 commented Apr 19, 2022

@masterix21
here I have all things work very good but my problem is just I can`t migrate tenant database tables after the tenant is created
here is my code

protected static function booted()
    {
        static::creating(fn(User $model) => $model->createDatabase());
    }

    public function createDatabase()
    {
        // add logic to create database

        DB::connection()->statement('CREATE DATABASE tenant_' . $this->username);
        $this->database = 'tenant_' . $this->username;
        Artisan::call('tenants:artisan migrate --tenant=' . $this->id);
    }

@masterix21
Copy link
Collaborator

How can you call $this->id if createDatabase() is called during the creating event?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests