PHP code example of envor / laravel-database-manager

1. Go to this page and download the library: Download envor/laravel-database-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

envor / laravel-database-manager example snippets


// config for Envor/DatabaseManager
return [
    /***
     * The disk where the sqlite database files will be stored.
     */
    'sqlite_disk' => 'local',

    /***
     * Available drivers that can be managed.
     */
    'managers' => [
        'sqlite' => \Envor\DatabaseManager\SQLiteDatabaseManager::class,
        'mysql' => \Envor\DatabaseManager\MySQLDatabaseManager::class,
    ]
];

// controller
public function store(Request $request)
{
    $this->validate($request->all());

    $databaseManager = (new Envor\DatabaseManager)
        ->manage($request->database_driver)
        ->createDatabase($request->database_name);
    
    if($databaseManager){
        $request->user()->databases()->create([
            'name' => $request->database_name,
            'driver' => $request->database_driver,
        ]);
    }
}

    // test
    public function test_it_can_create_a_database(): void
    {
        $this->actingAs($user = User::factory()->create());

        Envor\DatabaseManager\Facades\DatabaseManager::fake();

        $this->post(route('database.create'), [
            'database_driver' => 'sqlite',
            'database_name' => 'test_database',
        ]);

        $this->assertDatabaseHas('databases', [
            'user_id' => $user->id,
            'driver' => 'sqlite',
            'name' => 'test_database',
        ]);
    }

$databaseManager = (new Envor\DatabaseManager)
    ->manage('sqlite')
    ->createDatabase('my-new-database');

echo now()->format('Y/m/d_h_i_s_');
// 2023/3/2/7_04_38_
$databaseManager->deleteDatabase(
    databaseName: 'my-new-database', 
    deletedAt: now(), // optional: defaults to now() (Carbon date)
);

// erase the database permanently from disk
$databaseManager->eraseDatabase('.trash/2023/03/02/07_04_38_my-new-database');

$databaseManager->cleanupOldDatabases(
    daysOld: 1, // optional, defaults to one
);

$databaseManager = (new Envor\DatabaseManager)
    ->manage('mysql')
    ->setConnection('any-mysql-connection')
    ->createDatabase('my_new_database');

echo now()->format('Y_m_d_h_i_s_');
// 2023/3/2/7_04_38_
$databaseManager->deleteDatabase(
    databaseName: 'my_new_database', 
    deletedAt: now(), // optional: defaults to now(). Uses Carbon. 
);

$databaseManager->cleanupOldDatabases(
    daysOld: 1, // optional, defaults to one
);
bash
php artisan vendor:publish --tag="database-manager-config"