Comments (9)
This is greasy, but it works in controller:
exec('cd ' . base_path() . ' && php artisan tenants:artisan "migrate --seed" --tenant=' . $tenant->id);
from laravel-multitenancy.
Hmm, I think I overlooked the simplicity of $tenant->makeCurrent()
.
That said, still banging on correct syntax for the Artisan call...
Artisan::call('tenants:artisan', [
'migrate' => true,
'--seed' => true,
'--tenant' => [$this->tenant->id],
]);
Symfony\Component\Console\Exception\CommandNotFoundException: The command "tenants:artisan migrate --seed" does not exist. in /home/vagrant/code/sites/troovy/vendor/laravel/framework/src/Illuminate/Console/Application.php:181
from laravel-multitenancy.
Update. This seems to run properly:
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->tenant->id);
However, I now get this:
Next Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000] [1049] Unknown database 'a0eb5d78-9c85-48ae-8d21-4981a4d93643' in /home/vagrant/code/sites/troovy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31
I have a tenant prefix of 'tenant_' on tenant databases. Does the Artisan command just look at $tenant->database perhaps, without adding prefix? Checking...
Update. Tried inserting 'tenant_' . Str::uuid() instead of just UUID into Tenants db. Same error:
PDOException: SQLSTATE[HY000] [1049] Unknown database 'tenant_2e116d05-e941-4721-9212-4749743d4329' in /home/vagrant/code/sites/troovy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27
Update 2. Confirmed, also fails in console:
SQLSTATE[HY000] [1049] Unknown database '8b3c0438-7dc3-4f4d-a652-93aea2e60ac2' (SQL: select * from information_schema.tables where table_schema = 8b3c0438-7dc3-4f4d-a652-93aea2e60ac2 and table_name = migrations and table_type = 'BASE TABLE')
from laravel-multitenancy.
Ultimately, I think I tracked it down to a missing config setting. Didn't have the default connection set to 'tenant'.
from laravel-multitenancy.
Hey @dambridge , how did you fix the command does not exist
issue?
My artisan call is pretty much exactly like yours:
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->id);
But I'm still getting:
Symfony\Component\Console\Exception\CommandNotFoundException The command "tenants:artisan" does not exist.
This is how my createDatabase()
method looks like:
public function createDatabase()
{
DB::statement('CREATE DATABASE `'.$this->database.'`');
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->id);
}
from laravel-multitenancy.
Still working through this. This call works perfectly when I have it in a job (I have made all jobs tenant aware by default in the config).
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->tenant->id);
However... it fails when I run it in a controller directly with the same error! Very strange, still looking at it.
from laravel-multitenancy.
If it works in my job, then something about environment/context must be at play is my thinking. Not sure if this is relevant, but found this in the MultiTenancy service provider:
if ($this->app->runningInConsole()) {
$this
->registerPublishables()
->bootCommands();
}
protected function bootCommands(): self
{
$this->commands([
TenantsArtisanCommand::class,
]);
return $this;
}
Maybe this means the command is only available from within console? And because jobs run 'out of process' it works?
from laravel-multitenancy.
In the vendor of your app, move that bootCommands
outside of runningInConsole
and see if just using Artisan::call
. work for the command. If it does, I'd accept a PR that makes this change in the package 👍
from laravel-multitenancy.
After moving the bootCommands
outside of runningInConsole
I got the Artisan ::call
to work IF in a controller.
It does not work if called inside the Tenant
model.
If possible I'd like to create the database AND run the migration inside the createDatabase()
method on my Tenant
model, as suggested in the documentation.
Is this possible?
from laravel-multitenancy.
Related Issues (20)
- Tenant-Aware Job Scheduling with Horizon HOT 4
- MakeQueueTenantAwareAction seems to break active requests when Queue Connection is sync HOT 4
- base table or view not found with AWS SQS queue for job TenantAware HOT 15
- randomly receive Invalid catalog name: 1046 No database selected (Connection: mysql, SQL: insert into failed_jobs (uuid, connection, queue, payload, exception, failed_at) on queue sqs HOT 5
- Tenant Aware Queue Fails No Database Selected when running from supervisor in a multi tenant environment using AWS SQS FIFO HOT 2
- Update documentation at https://spatie.be to `v3` HOT 3
- in_array(): Argument #2 ($haystack) must be of type array, null given {"exception":"[object] (TypeError(code: 0): in_array(): Argument #2 ($haystack) must be of type array, null given at /spatie/laravel-multitenancy/src/Actions/MakeQueueTenantAwareAction.php:79) HOT 1
- Cache issue with spatie/laravel-permission in TenantAware Command HOT 5
- Issue with the has function on different connections HOT 1
- I want to Connect to mongo DB for some tables but it is giving "Database connection [mongodb] not configured.", exception: "InvalidArgumentException",…} error
- Custom cache driver missing when switching Tenant HOT 4
- Cannot schedule jobs in Laravel 11 HOT 3
- Can't make route not tenant aware
- Erro during Laravel 11 for Notification multitenacy for multi-database
- Problem to install in Laravel 11 HOT 1
- Laravel 11: Multiple database does not switch and use tenant database HOT 2
- NeedsTenant with route model binding HOT 4
- Spatie\Multitenancy\Concerns\UsesMultitenancyConfig should be moved to ImplementsTenant? HOT 2
- getConnectionName() not compatible - ?string
- Session store not set on request HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-multitenancy.