I'm creating a multi-tenant application with Laravel 4. At the moment I have two connections in my database.php
'connections' => array(
'app' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
When connecting to a tenant database in the application I call something like this:
$tenant = Tenant::where('username', '=', $username)->first();
Config::set('database.connections.tenant.username', $tenant->db_username);
Config::set('database.connections.tenant.password', $tenant->db_password);
Config::set('database.connections.tenant.database', $tenant->db_database);
As you can see all my database names/connection details for tenants are set dynamically.
this is an issue when i'm trying to run a migration as I can't find a way to set the connection details.
The only way I can do this at the moment is to call my migration from a controller. Is their a better way?
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);
I am working on a new application that must connect to several databases. The databases are dynamically created when a new company is registered in my application i.e each company have there on databases.
My question is how to establish the database connection using laravel,
If the company admin is login he needs to connect the corresponding db. How is it possible ???
I know how to establish multiple database connection if the databases are stable, like,
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge1'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge2'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
but if the databases are not stable how is it possible ????
If my question is not up to standard, please forgive me
You can add dynamically the database configuration like this.
if(NULL === config()->get('database.connections.company')){
config()->set('database.connections.company', [
'driver' => $companyDbDriver,
'host' => $companyDbHost,
'port' => $companyDbPort,
'database' => $companyDbName,
'username' => $companyDbUser,
'password' => $companyDbPassword,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
]);
}
in the model; set the connection attribute to 'company'
/**
* Connection configuration ID.
*
* #var string
*/
protected $connection = 'company';
there are complication if you dont load the configuration of the database before calling the models using it, i'll leave that to you.
First you have to know where your database connections parameters are stored.
Anyway, wherever they are, you should create a new connection on the fly.
Supposing each User has it's own connection ...
<?php
use Illuminate\Support\Facades\Config;
$user = Users::find($id);
Config::set('database.connections.' . $user->username, array(
'driver' => 'mysql',
'host' => $user->db_host,
'database' => $user->db_name,
'username' => $user->db_user,
'password' => $user->db_pass,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
));
// And set the new connection to interested models
$myModel = new MyModel;
$myModel->setConnection($user->username);
$data = $myModel->where(...)->get();
Hope this help
I am new to Laravel used this https://github.com/msurguy/laravel-facebook-login link to work with facebook login in Laravel. I have setup all the things but i
got following errors when establishing database connection :
Type error: Argument 1 passed to
Illuminate\Redis\Database::__construct() must be of the type array,
null given, called in
C:\wamp64\www\laravel-facebook-login-master\vendor\laravel\framework\src\Illuminate\Redis\RedisServiceProvider.php
on line 23
I want to run simple mysql database. Below is my database.php code:
return array(
'fetch' => PDO::FETCH_CLASS,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'facebookdb',
'username' => 'root',
'password' => '',
'port' => '3603',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
'migrations' => 'migrations',
);
Any suggestion/help will be highly appreciated.
You are missing the definition for your redis connection in that connections array, something like this will help:
'redis' => array(
'host' => env('REDIS_HOST', 127.0.0.1),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
'database' => env('REDIS_DATABASE', 0)
)
Make sure that you add the corresponding entries in your .env file if your redis connection settings differ from the default values above.
I want to store multiple database connection in my current laravel db.
i need to connect these via dropdown and generate reports on the fly.
what is the best way to configure this
You can just create a table "connection(strings)" in your 1st db and create a model for that table.
After that you can do something like this:
$selected = Connection::query()->where('name', 'fromdropdown')->first();
$connection = $selected;
config(['database.connections.data' => array(
'driver' => 'sqlsrv',
'host' => $connection['Database_Server'],
'database' => $connection['Database_Name'],
'username' => $connection['Database_User'],
'password' => $connection['Database_Pass']
)]);
DB::setDefaultConnection('data');
You can set multiple database connections in the app/config/database.php file.
Example:
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And then just use the connection name in your query:
$users = DB::connection('mysql2')->select(...);
If I'm doing something simple like
Auth::user()->username
Is there a way I can change the database that I'm getting this information from?
I already have the other database connection information entered in the database.php file. Just not sure how to change it on the fly.
DB SETUP
'mysql2' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In your AUTH driver specify the DB to use
app/config/auth.php
'driver' => 'database',
for diff table than users
'table' => 'administrators',
Create your own driver
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver