Skip to content
Advertisement

connect laravel application with database based on the url

hi im using laravel 6 project i want to connect my application to database based on the url like this schema

www.mywebsite.com/project1
www.mywebsite.com/project2
www.mywebsite.com/project3
www.mywebsite.com/project4

and i did this steps in my config/config.php

<?php
return 
[
    'application_path' => explode('/',$_SERVER['REQUEST_URI'])[1],
];

i did this code to use the $application_path in controllers like this code

$file->move(public_path().'/companys/'.config('global.application_path')

and in my AppServiceProvider.php i did this code

$url = $_SERVER['REQUEST_URI'];
$url = explode('/',$url);
$urls = $url[1];
View::share('application_path', $urls);

to use $application_path inside the blade like this

href='{{ asset("/companys/".$application_path."/uploads/archives/$request->user_id/$file_name.$ex")}}'

and in my config/database.php i did this code

$url = $_SERVER['REQUEST_URI'];
$url = explode('/',$url);
$database = 'mycpanelusername_'.$url[1];
$username = 'myusername';
$password = 'mypassword';
'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => $database,
        'username' => $username,
        'password' => $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'),
        ]) : [],
    ],

and i have databases called

project1 project2 project3

so what im trying is when im visit www.mywebsite.com/project1 connect me with database project1 and use the folder companys/project1 as the public folder and do the same for other links project2 project3 ect .. now when i upload the files to the cpanel everything is working till it redirect me to the login url with loading and un loaded loading blank page .. any help here thanks a lot

Advertisement

Answer

I think this answer in the other question would help you out!: https://stackoverflow.com/a/31847198/8219427

If not, then there is official Laravel documentation.: https://laravel.com/docs/database#using-multiple-database-connections

Ps.: Use the framework provided options, example get the uri from request object! https://laravel.com/docs/requests example:

use IlluminateHttpRequest;

Route::get('/', function (Request $request) {
    $uri = $request->path();
});

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement