我想使用Laravel 5.3中的刀片通过表单上的字段连接到数据库。但我真的不知道我是怎么做到的。 我会输入主机,用户,密码和数据库名称,当我按下按钮时,它会在屏幕上显示数据库表。 我的代码没有连接,只是为了举例说明:
<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>
答案 0 :(得分:1)
我从问题中了解到您希望动态建立数据库连接,以便您可以像这样创建一个用于即时连接的类
<?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?
答案 1 :(得分:0)
我删除了表单,然后点击我的controller create
$tables = DB::select('SHOW TABLES');
return view("geradors.create",['tables'=>$tables]);
在我的view
中,我选择了我想要使用的表格。很容易。
但感谢您的回复。帮助了我很多。