I build an application for many tenants and each tenant has it's own database. The name of the database is the same as the tenants id.(the id exists out of five numbers). After authentication the user should be connected with his own database and redirected to his dashboard.
I tried to connect the user with his database with the following code, which I placed in Authenticate.php
if (!Auth::guest() && Auth::user()->tenant) {
$user = Auth::id();
Tenanti::driver('user')->asDefaultDatabase($user, 'users_{id}');
Config::set('database.connections.mysql.database', 'user_'.$user);
}
The if statement checks in the main database if the logged in user is a tenant(boolean).
The config/database.php contains the following code:
'tenants' => [
'user_1' => [
'driver' => 'mysql',
'host' => 'localhost', // for user with id=1
'database' => '86097', // for user with id=1
'username' => 'root', // for user with id=1
'password' => 'root', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
AppServiceProvider:
<?php namespace App\Providers;
use Orchestra\Support\Facades\Tenanti;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Tenanti::setupMultiDatabase('tenants', function (User $entity, array $template) {
$template['database'] = "user_{$entity->getKey()}";
return $template;
});
}
}
?>
I don't get an error, but the connection hasn't changed. The user database is empty and therefore I shouldn't see any data when I log in with user_id=1 . Thanks in advance for helping.
The configuration should be:
'tenants' => [
'driver' => 'mysql',
'host' => 'localhost', // for user with id=1
'database' => '86097', // for user with id=1
'username' => 'root', // for user with id=1
'password' => 'root', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Other than that please replace Tenanti::setupMultiDatabase() with Tenanti::connection() (we have deprecated the old method).
And change the following:
Tenanti::driver('user')->asDefaultConnection(Auth::user(), 'tenants_{id}');
Obviously if you want to use users_{id} you would then need to replace all tenants to users.
Related
In a multi-tenant Laravel app, each tenant has its own database connection. So after the user has selected his database, I want to authenticate the user using Auth::loginUsingId. Still, no matter what I do, I cannot change the Users Model's connection to another default.
If I set the connection in the model, it does connect to the specific database, but I want this to be done dynamically.
Is there a way to specify the connection dynamically that Laravel's auth should use for the authentication?
You could define another connection in your config/database.php file like this:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'second_db_connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And change the User model to be like this:
class User extends Model {
protected $connection = 'second_db_connection';
}
Before authenticating the user, change the database connection temporarily for the current request only using Conifg::set
$db = "database_name";
Config::set("database.connections.mysql.database", $db);
How to connect two database in laravel-5 and how to get data from db.
I know two one thing for that
In config/database set like this.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop2'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Using Query you can define a connection on the Query Builder:
$users = DB::connection('mysql2')->select('your query');
Using Eloquent
You can also define which connection to use in your Eloquent models as well!
<?php
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
You can also define the connection at runtime via the setConnection method.
<?php
class SomeController extends BaseController {
public function someMethod()
{
$someModel = new SomeModel;
$someModel->setConnection('mysql2');
$something = $someModel->find(1);
return $something;
}
}
I have find the issue of connect 2 database in laravel.
If any body wants two work with 2 database then config as par above suggestion and remove database configuration from .env file which is located at root.
I have tried and I am working with 2 database.
Do i need to create multiple connection to access different database/schema. Cant I use with one dbconnection. Is there a way to pass the database name in the laravel eloquent or db builder? Currently in raw php i use one connection to query the different schema.
Create different connections to your database.php file and then pass them to your eloquent models.
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
// connection 2
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Suppose i have model User.php uses mysql connection named mysql1
inside my model i will add :
protected $connection = 'mysql1';
if i want to use mysql connection named mysql2
then i will use
protected $connection = 'mysql2';
Here i am setting connections statically in to models.
In Eloquent, I use DB:connection() to set my named connection, like so:
$query = DB::connection('db_connection_name')->table($this->table)
If you weren't aware, the database connections are named in the config app/config/database.php
I'm trying to change the connection on the saving event. Here is what I have done so far:
I make and event on the saving method:
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
$model->connection = 'test';
var_dump($model);
return $model;
});
}
And then I make a save on User.
$user = new User();
$user->save();
When that is done, it still uses the default connection instead of the test-connection I have set.
I have both a default and a test connection configured in app/config/database.php which you can see below: (OBS: The change is the prefix)
// ...
'connections' => array(
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'test' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'test_',
),
),
// ...
The var_dump turns out with the following, where you can see that the $model->connection have been changed.
object(User)[159]
...
protected 'connection' => string 'default' (length=7)
...
But for some reasons it stills goes into the table users instead of test_users.
Does anyone have any idea how to make this work?
I figured out a way to do this by overriding Eloquent's save-method.
public function save(array $options = array())
{
// set draft connection
Config::set('database.default', 'test');
// do the actual save
parent::save($options);
// change back to original connection
Config::set('database.default', 'default');
}
I'm working on a Laravel 4 based site which works over multiple databases. There is one query I need to run on each request that pulls a record from a different database.
Is there a way I can somehow tie this particular model to the other database so I can just retrieve it as usual?
$client = Client::find(Session::get('client_id'));
Any advice appreciated.
Thanks
// Model
class Client extends Eloquent
{
protected $connection = 'masterDb';
}
// config/database.php
'masterDb' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'name',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
You can create as many named connections as you wish. Set one of them as default,
each model can use any of these connections later.