I am having some trouble with disconnecting my created conn.
$dbName = session("key")["db_name"]["0"]->main_db;
$oldEmail = $this->connection()->table("tbl_user")->where("id",$UserID)->get();
DB::disconnect($dbName);
$User = User::where("email",$email)->get();
return $User;
I think the connection bypass my model and search for the table name of "User" in my created connection, I don't get the error, though it has the same name in my migration.
This is my codes in generating another connection:
public function connection() {
// SESSION
$dbName = session("key")["db_name"]["0"]->main_db;
// MAKE CONNECTION
Config::set("database.connections.".$dbName, array(
"driver" => config("app.DB_CONNECTION"),
"host" => config("app.DB_HOST"),
"database" => $dbName,
"username" => config("app.DB_USERNAME"),
"password" => config("app.DB_PASSWORD"),
));
DB::setDefaultConnection($dbName);
$conn = DB::connection($dbName);
return $conn;
}
app/config/database.php
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Useful Links
https://laracasts.com/discuss/channels/eloquent/laravel-5-multiple-database-connection
Related
ers. I need to change the database name into a specific controller. I already changed the database.php into this
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'loal'),
'database' => env('DB_DATABASE', 'test1'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'local'),
'database' => env('DB_DATABASE', 'test2'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
My main DB is test1 and I need to change it into test2 db name in here :
public function TransactionHistory(Request $request){
Config::set('database.default','sqlsrv2');
dd(DB::connection() );
}
But it only returns null and it is still reading test 1. Anyone?
One way is to change the connection by using DB::connection() method:
$connection = DB::connection('sqlsrv2'); //this will create a database connection using sqlsrv2 in your config.
Now, you can use $connection to run the queries, etc.
Reference :
If you are using Eluquent there is more elegant way Database Connection
Create enother config for second connection
`
'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' => '',
),
);
`
Set connection in your Model:
`
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The connection name for the model.
*
* #var string
*/
protected $connection = 'connection-name';
}
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.
This is my database.php file:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'learning_laravel',
'username' => 'forge',
'password' => 'bomb',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
My route.php file:
<?php
Route::get('/',function(){
$username=DB::table('username')->get();
return $username;
});
I created a database called learning_laravel and created a table called username and given input to it but i am not getting any output.
When i open localhost:8000/ by typing php artisan serve this is the output i get.
My Output file:
Database[]not configured
$connections = $this->app['config']['database.connections'];
if (is_null($config = array_get($connections, $name)))
{
throw new \InvalidArgumentException("Database [$name] not configured.");
}
You haven't your database connection configured.
You can find config in following file app/config/database.php with example content
'default' => 'mysql', //your database name according to prefix in connections subarray
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name here',
'username' => 'your username here',
'password' => 'your password here',
'charset' => 'utf8',
'collation' => 'utf8_bin',
'prefix' => '',
),
My problem is that I get the following error in MySqlConnector.php.
Undefined variable: host error
I am using two different connections for both read/write. I merged them in config/database.php file depending on environment that system uses. Here is the mysql connection codes.
<?php
switch($_SERVER['LARAVEL_ENV']){
case 'production':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'beta':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'development':
$connections = array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
}
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => $connections,
'migrations' => 'migrations',
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
All I wanted to do is using a different host for reading and another one for writing. When I use one connection in localhost, I get no error. But in multiple connections, I get the error. What is the reason for the error?
Ok I found the problem. It was because of laravel version. I updated laravel 4.0 to 4.1 and problem was solved.
just for the record of your environment implementation.
You have a build in laravel the environmen detection and loading of specific conf files.
you can read here: http://laravel.com/docs/configuration#environment-configuration
but the general idea is to define your environments in the :
$env = $app->detectEnvironment(array(
'local' => array('your-local-machine-name'),
'production' => array('your--prod-machine-name'),
));
and than you go to config folder and create inside it a folder foreach environment you have.
create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override.
implement this, and than check your errors back here again
I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.