Laravel dynamically adds or modifies database configuration

Use Laravel service providers to dynamically modify or add data configurations

DatabaseServiceProvider

extended configuration file

Create a Database service provider

php artisan make:provider DatabaseServiceProvider

Load (register) service provider, two ways

Method 1. Register in AppServiceProvider

<?php

namespace App\Providers;
...
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    
    
        //
        $this->app->register(DatabaseServiceProvider::class);
    }

Method 2. Add globally in confg/app.php

'providers' => [
    ...
    App\Providers\DatabaseServiceProvider::class,

Extend the database configuration in DatabaseServiceProviderthe service provider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class DatabaseServiceProvider extends ServiceProvider
{
    
    
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
    
    
        //
        $new = [
            'mydb-test' => [
                'driver' => 'mysql',
                'host' => 'docker.app',
                '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' => '',
                'strict' => true,
                'engine' => null,
            ]];
        $this->app['config']['database.connections'] = array_merge($this->app['config']['database.connections'], $new);

    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
    
    
    }
}

For testing, just connect to the database in tinker

The above code can already meet the requirements, but it is not coquettish enough.

The following code my-testis the connection subscript, and the set method will automatically set the corresponding array subscript.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class DatabaseServiceProvider extends ServiceProvider
{
    
    
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
    
    
	    $new = [
                'driver' => 'mysql',
                'host' => 'docker.app',
                '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' => '',
                'strict' => true,
                'engine' => null,
            ];

        $this->app->get('config')->set('database.connections.mydb-test', $new);
	}
	
	
	/**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
    
    
    }
}

For the rest, you can use tinker to test whether the database connection has been added successfully.

Guess you like

Origin blog.csdn.net/q116975174/article/details/103958764