L5.1 Where To Store DB Connection For Package? - database

In my package, I've own config file and database connection file. Merging config with the app config is quit simple, publishing data migration files too. But how can I merge the database connection file in my Service Provider with the app /config/database.php?
/packages/el/adm4y/src/config/database.php
return [
'connections' => [
'adm4y' => [
'driver' => 'mysql',
'host' => env('DB_ADM4Y_HOST', 'localhost'),
'database' => env('DB_ADM4Y_DATABASE', ''),
'username' => env('DB_ADM4Y_USERNAME', ''),
'password' => env('DB_ADM4Y_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => false,
]
]
];

Related

Laravel connection with local SQL instance with Windows Authentication

I am trying to establish connection with SQL Server local instance in Laravel. The SQL Server has Windows Authentication.
'driver' => 'sqlsrv',
'host' => 'SKILL....\SQLEXPRESS',
'port' => '1433',
'database' => 'dbname',
'username' => 'SKILL....\101...',
'password' => 'W.....',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'prefix_indexes' => true,
Unable to establish connection.

How to use multiple db2 databases in Laravel

Currently i use the package cooperl/laravel-db2 for the database connection. For my project, i should to toggle schema database.
I try this in db2.php :
'connections' => [
'ibmi_1' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_1',
'port' => *****,
'date_format' => 'Y-m-d H:i:s',
...
],
'ibmi_2' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_2',
'port' => *****,
...
],
And this in .env
DB_CONNECTION=ibmi_1
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
DB_CONNECTION=ibmi_2
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
But when i try in tinker
DB::connection('ibmi_2')
I have this error
InvalidArgumentException with message 'Database connection [ibmi_2] not configured.'
What is wrong ?
The problem was that the database configuration wasn't in bootstrap/cache/config.php.

I can't migrate dynamically with Laravel

I am creating a database for each user who has a record. I am able to set this created database via config. But I can't migrate.
The error I encountered is:
Database [5d7acdf5adf6a] not configured.
$user=User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'database'=>uniqid()
]);
Artisan::call('make:database',['dbname'=>$user->database]);
$this->connectAnother($user->database);
Artisan::call('migrate',['--database'=>$user->database]);
dd(Artisan::output());
error-->Database [5d7acdf5adf6a] not configured.
connectAnother is a method I wrote.
public function connectAnother($database){
\Config::set("database.connections.mysql", [
"host" => "127.0.0.1",
"database" => $database,
"username" => "...",
"password" => "..."
]);
}
config/database.php:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
I tried to migrate directly when I changed the database, but got the answer 'Nothing to migrate'
Here is the solution
public function connect($database){
$connection = [
'driver' => 'mysql',
'url' => config('database.connections.mysql.url'),
'host' => config('database.connections.mysql.host'),
'port' => config('database.connections.mysql.port'),
'database' => $database,
'username' => config('database.connections.mysql.username'),
'password' => config('database.connections.mysql.password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
];
\Config::set("database.connections.{$database}", $connection);
DB::setDefaultConnection($database);->***The most important place in this line***

Undefined class constant 'SQLSRV_ENCODING_SYSTEM'

when trying to run php artisan migrate on laravel i'm getting the error
In database.php line 79:
Undefined class constant 'SQLSRV_ENCODING_SYSTEM'
i have the sql drivers etc installed but unsure how to solve this problem
database.php file
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'options' => [PDO::SQLSRV_ENCODING_SYSTEM => false], // Used for MSSQL encoding
],
Explanations:
PDO::SQLSRV_ENCODING_SYSTEM is PDO_SQLSRV Driver Constant, not an option name. You should use PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_SYSTEM in options. Have in mind, that this option is Microsoft Drivers for PHP for SQL Server specific driver attribute (one of PDO::SQLSRV_ATTR_ENCODING and PDO::SQLSRV_ATTR_DIRECT_QUERY).
<?php
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'options' => [PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_SYSTEM]
],
...
?>
Notes:
Documentation: Microsoft SQL Server Functions (PDO_SQLSRV) and Constants (Microsoft Drivers for PHP for SQL Server).

Laravel Unit Test separate database

Can somebody help me, how can i run my tests, but they will interact with database copy or a virtual database. I know, that i need to use phpunit.xml file, but how? Give me a example please.
I have found a workaround for this.
In your config/database.php bellow mysql add:
'mysql_testing' => [
'driver' => 'mysql',
'host' => env('TESTING_DB_HOST', 'localhost'),
'database' => env('TESTING_DB_DATABASE', 'forge'),
'username' => env('TESTING_DB_USERNAME', 'forge'),
'password' => env('TESTING_DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And now in your .env add the values for:
TESTING_DB_HOST=localhost
TESTING_DB_DATABASE=homestead_testing
TESTING_DB_USERNAME=homestead
TESTING_DB_PASSWORD=secret
Now you can run
php artisan migrate —database=mysql_testing
And the last thing - open your phpunit.xml file in the app folder and add this:
<env name="DB_CONNECTION" value="mysql_testing"/>
<?php
return [
// other stuff
'default' => env('DB_DEFAULT', 'mysql'),
'connections' => [
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
// other stuff
];
For controller
public function createApplication()
{
putenv('DB_DEFAULT=sqlite_testing');
$app = require __DIR__ . '/../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
return $app;
}
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
#pardeep in test\CreatesApplication trait there is just one function
add this putenv('DB_DEFAULT=sqlite_testing');

Resources