one laravel folder with many database based on link

Viewed 45

7 project lets say im working with locahost i want if the url is http://localhost/project1 then the database will be project1 and if the link is http://localhost/project2 the database will be project2 ect any help here thanks a lot i tried to do this inside database.php

    $url = $_SERVER['REQUEST_URI'];
    $url = explode('/',$url);
    $url = $url[1];



    'mysql' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => $url,
        'username' => 'root',
        'password' =>'secrit',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

its working if i only enter the main path of laravel project thanks a lot

1 Answers

In Laravel you can set the database connection dynamically with:

Config::set("database.connections.mysql", [
    "host" => "...",
    "database" => "...",
    "username" => "...",
    "password" => "..."
]);

So of you have a Route where the first part is your project you could set the database connection in your Controller. Otherwise you could try to use Events to set the database connection with a Kernel-Event.

Related