0

I would like to connect to the DB through the fields on my form using blade from Laravel 5.3. But I don't really have idea how I do this. I would type the host, user, password and DB name and it would bring me the DB tables on the screen when I press the button conect. My code without connection, just to exemplify:

<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>
Lara Gallassi
  • 157
  • 1
  • 8

2 Answers2

1

I understand from the question that you want to make datbase connection on the fly so you can create a class for on fly connection like this

<?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);
    }
}

and then after user submit the connection settings you can call it

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

Or if you are looking to get this as installer for your application , you should save those connection settings to .env file on the application root folder

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

Ahmed Bebars
  • 367
  • 1
  • 13
0

I removed the form, and just put on my controller create

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

And in my view, I select the table I want to use. Very easy. But thanks for your response. Helped me a lot.

Lara Gallassi
  • 157
  • 1
  • 8