Here is a concept that might help you.
https://divinglaravel.com/understanding-how-laravel-configures-database-connections
However, you have to pass the database each time you make a request
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, i'm fairly new to laravel but not at all with php. I'm used to handling everything , and now i'm kind of lost with laravel doing all the work for me :D
I set up an authentification. On the login view i added a select which allows me to choose which database to query. (basically i have a db per corporation using the tool).
What i want to do , is ,setting the database to use, dynamically, at login. So user fill the form , clic the 'login' button and before anything i should define the database to use according to what is selected in the login form.
From what i understand this is possible by using something like :
Config::set('database.connections.mysql.database', 'DB_SELECTED_AT_LOGIN');
1- Is this correct ?
2- If so, is it mandatory to completly rewrite the LoginController and redefining routes as explained here : https://laravel.com/docs/6.x/authentication#authenticating-users ?
i don't see any other option to get the additional parameter from the login form
3- once i manage to get the parameter containing the db name how do i persist this setting during the user session ? The middleware solution given by @fylzero in this thread : https://laracasts.com/discuss/channels/laravel/laravel-dynamic-database-connections seems to be a good solution but , in my case i'm before the authentification process.
Thanks for your help
@grunk what about:
View (add new field):
<div class="form-group row">
<label for="database" class="col-md-4 col-form-label text-md-right">{{ __('Database') }}</label>
<div class="col-md-6">
<select name="database" class="form-control @error('database') is-invalid @enderror" required>
<option {{ old('database') === 'db1' ? 'selected' : '' }} value="db1">DB1</option>
<option {{ old('database') === 'db2' ? 'selected' : '' }} value="db2">DB2</option>
</select>
@error('database')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
LoginController:
class LoginController extends Controller
{
use AuthenticatesUsers {
attemptLogin as baseAttemptLogin;
}
protected $redirectTo = RouteServiceProvider::HOME;
protected function attemptLogin(Request $request)
{
config(['database.connections.mysql.database' => $request->database]);
return $this->baseAttemptLogin($request);
}
protected function authenticated(Request $request, $user)
{
session(['database' => $request->database]);
}
protected function validateLogin(Request $request)
{
$request->validate([
'database' => [
'required',
Rule::in(['db1', 'db2']),
],
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
}
app\Http\Kernel.php:
protected $middlewareGroups = [
'web' => [
//...
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\SetDBConnection::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//...
],
/...
];
app/Http/Middleware/SetDBConnection.php:
namespace App\Http\Middleware;
use Closure;
class SetDBConnection
{
public function handle($request, Closure $next)
{
if (session('database')) {
config(['database.connections.mysql.database' => session('database')]);
}
return $next($request);
}
}
Please or to participate in this conversation.