gpt4 book ai didi

html - laravel 5按钮连接数据库

转载 作者:行者123 更新时间:2023-11-29 11:03:22 25 4
gpt4 key购买 nike

我想使用 Laravel 5.3 中的 Blade 通过表单上的字段连接到数据库。但我真的不知道我该怎么做。我会输入主机、用户、密码和数据库名称,当我按下“连接”按钮时,它会在屏幕上显示数据库表。我的代码没有连接,只是为了举例:

<div class="row">
<div class="col-lg-3">
{!! Form::label('sevidor', 'Servidor:', ['class' => 'control-label']) !!}
{!! Form::text('sevidor', '127.0.0.1', ['class' => 'form-control']) !!}
</div>
<div class="col-lg-3">
{!! Form::label('bancodedados', 'BD:', ['class' => 'control-label']) !!}
{{ Form::text('bancodedados', null, ['class' => 'form-control']) }}
</div>
<div class="col-lg-3">
{!! Form::label('usuario', 'Usuário:', ['class' => 'control-label']) !!}
{!! Form::text('usuario', 'root', ['class' => 'form-control']) !!}
</div>
<div class="col-lg-3">
{!! Form::label('senha', 'Senha:', ['class' => 'control-label']) !!}
<div class="input-group">
{{ Form::text('senha', null, ['class' => 'form-control']) }}
<span class="input-group-btn">
{!! Form::button('Conectar', ['class' => 'btn btn-info']) !!}
</span>
</div>
</div>
</div>

最佳答案

我从问题中了解到您想要动态建立数据库连接,这样您就可以为这样的动态连接创建一个类

<?php namespace App\Database;

use Config;
use DB;

class OTF {

/**
* The name of the database we're connecting to on the fly.
*
* @var string $database
*/
protected $database;

/**
* The on the fly database connection.
*
* @var \Illuminate\Database\Connection
*/
protected $connection;

/**
* Create a new on the fly database connection.
*
* @param array $options
* @return void
*/
public function __construct($options = null)
{
// Set the database
$database = $options['database'];
$this->database = $database;

// Figure out the driver and get the default configuration for the driver
$driver = isset($options['driver']) ? $options['driver'] : Config::get("database.default");
$default = Config::get("database.connections.$driver");

// Loop through our default array and update options if we have non-defaults
foreach($default as $item => $value)
{
$default[$item] = isset($options[$item]) ? $options[$item] : $default[$item];
}

// Set the temporary configuration
Config::set("database.connections.$database", $default);

// Create the connection
$this->connection = DB::connection($database);
}

/**
* Get the on the fly connection.
*
* @return \Illuminate\Database\Connection
*/
public function getConnection()
{
return $this->connection;
}

/**
* Get a table from the on the fly connection.
*
* @var string $table
* @return \Illuminate\Database\Query\Builder
*/
public function getTable($table = null)
{
return $this->getConnection()->table($table);
}
}

然后在用户提交连接设置后您可以调用它

// Using the same host/passwword/etc, just changing databases

$otf = new App\Database\OTF([
'driver' => 'pgsql',
'database' => 'puppies',
'username' => 'jack',
'password' => 'the-cute-dog'
]);

// Get the users table
$users = $otf->getTable('users');

// Find the first user in the table
$first_user = $users->first();

https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5

或者,如果您希望将其作为应用程序的安装程序,则应将这些连接设置保存到应用程序根文件夹上的 .env 文件

How to change variables in the .env file dynamically in Laravel?

关于html - laravel 5按钮连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41836413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com