Connecting to Different Database in October CMS
Hello,
I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.
I want to connect to the other database and fetch information from one table and display on my home page.
Please help me with this. I am newbie to this CMS.
Thank you.
In your config/database.php you will have something like this (assuming MySQL):
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Just add another one after it, changing the name to something else (mysql-legacy in my example below) and the relevant connection details:
'mysql-legacy' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
After that, you can use DB; in your model or controller and then do things like this:
$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');
Related
I have two applications both developed in Laravel.I have logged into the first application and doing coding. But when I try to log into the second application it gives an error of the missing 'users' table(the table in the second database I am trying to log into now which is not in the first logged in an application).
Seems like the second application is trying to authenticate the user using the first database which is not correct
Can anyone assist me with this
Dear Laroja as i get you want to authenticate with another application which have separate database then you have to define two database auth and try to build the logic if connect to first application then it uses the first database and if you want to connect with another application it will connect to other database.
Please have a sapmle code for connect two databse
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'qmops_live_server',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => env('DB_PORT', '3306'),
'database' => 'XXX',
'username' => 'XXX',
'password' => 'QXXXX',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
please use the above code in app/config/database.php hope my solution will help you Thanks.
I have multiple database. I want to change db name based on the url dynamically. How can I set particular db before authentication.
I want to change database from authentication to through out the application.
For ex.
If url is like lara.local.com/comapny1
then it will select database company1
If url is like lara.local.com/company2
then it will select database company2
Based on the selected database authentication will be done and selected database will be used for that user.
Make entry for second database in config/database.php
'company1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'database1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'company2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
By default your queries will use the mysql connection to change connection to 'company1'
DB::connection('company1')->select($query);
Additionally you can set database connection for Model
$someModel = new MyModel;
$someModel->setConnection('company1');
You can use the Request::is() to get URI from URL
if(Request::is('company1')){
//change database to company1
Config::set("database.connections.company1.database", 'company1');
}
elseif(Request::is('company2'){
Config::set("database.connections.company1.database", 'company2');
}
You can achieve this thing with below steps:
1 Define multiple connection into database.php for example:
'db1' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db1_connection',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
2 Use connection based on conditional statement(You should move this code else)
//Check url that contains 'MYCODE'
if ('URL Contains' == 'MYCODE') {
DB::disconnect();
Config::set('database.default','db1');
DB::reconnect();
}
else {
DB::disconnect();
Config::set('database.default','db2');
DB::reconnect();
}
It should work for you, however I haven't tested it. Reference taken from answer by Marcin Nabialek
You can do this in AppServiceProvider's boot() like this
public function boot()
{
if($this->app['request']->getHost()=='test.com') {
Config::set('database.default','mysql');
}
else{
Config::set('database.default','mysql1');
}
}
I'm using Laravel's illuminate database library outside laravel with jessengers mongodb.
My requirement is multiple database connectivity through illuminate database.
Currently, I've added two connection one mysql and one mongodb.
To split the database load, I need to connect to mysql router instead of mysql db server directly. Also, in that I want one only for Read operation and one for Read/Write operation.
Kindly help me out on this.
Thanks in advance.
Current connections
$db = new Capsule;
$db->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'test',
'password' => 'test#123#',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "default");
$db->addConnection([
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => null,
'password' => null,
'options' => []
], "mongodb");
$db->getDatabaseManager()->extend('mongodb', function ($config) {
return new Connection($config);
});
$db->setEventDispatcher(new Dispatcher(new Container));
$db->setAsGlobal();
$db->bootEloquent();
I need to replace one mysql connection with two mysql connection for Read and Read/Write operation through mysql router.
You can define read/write options separately with either mysql host or mysql router host and port
$db->addConnection([
'driver' => 'mysql',
'read' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'write' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'database' => '<mysql_database>',
'username' => '<mysql_database_user>',
'password' => '<mysql_database_password>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "mysql");
So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.
I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.