代码之家  ›  专栏  ›  技术社区  ›  Lara Gallassi

laravel 5按钮连接数据库

  •  0
  • Lara Gallassi  · 技术社区  · 7 年前

    我想使用Laravel 5.3中的blade通过表单上的字段连接到DB。但我真的不知道如何做到这一点。 我会输入主机、用户、密码和数据库名,当我按下conect按钮时,它会在屏幕上显示数据库表。 我的代码没有连接,只是举个例子:

    <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>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Ahmed Bebars    7 年前

    我从这个问题中了解到,您希望动态创建datbase连接,以便可以像这样为动态连接创建一个类

    <?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?

        2
  •  0
  •   Lara Gallassi    7 年前

    我去掉了表格,然后把我的 controller create

    $tables = DB::select('SHOW TABLES');
            return view("geradors.create",['tables'=>$tables]);
    

    在我的 view ,我选择要使用的表。这很容易。 但谢谢你的回复。帮了我很多忙。