2017-01-24 58 views
0

我想通过使用Laravel 5.3中的刀片在表单上的字段连接到数据库。但我真的不知道我是如何做到这一点的。 我会输入主机,用户,密码和数据库名称,当我按下按钮时,它会将数据库表格带到我的屏幕上。 我的代码,而无需连接,只是为了举例说明:laravel 5按钮连接数据库

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

回答

1

我从这个问题明白,你想对飞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?

0

我删除了形式,只是穿上我controller create

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

在我view,我选择我想要使用的表。好简单。 但是,谢谢你的回应。帮了我很多。