html - laravel 5按钮连接数据库

标签 html mysql laravel laravel-blade

我想使用 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/

相关文章:

javascript - 使用 javaScript 隐藏和显示内容

php - Doctrine2 导致网站速度变慢

与缺少扩展相关的 PHP 错误/异常处理

css - 如何对齐两个 DIV 的顶行?

javascript - jquery点击按钮后获取最后激活的数据

mysql - SQL where 两个时间列之间的两个时间值

mysql - 如何从多个数据库动态获取数据?

php - Laravel 集合键修改

javascript - JQuery/Javascript - 如何根据相应的 <select> 更改 <label>

php - 24 小时日期增量 (PHP/Laravel)