3

So basically i'm building an application where the customer wants to connect to his own databases, and I made a form where he can submit his credentials

connectionForm: this.$inertia.form({
                driver: 'mysql',
                name: '',
                host: '',
                port: 3306,
                username: '',
                password: '',
                database: '',
                charset  : 'utf8',
                collation: 'utf8_unicode_ci',
                prefix: '',

            })

Array received in laravel controller

array:10 [▼
  "driver" => "mysql"
  "name" => "Connection"
  "host" => "127.0.01"
  "port" => 3306
  "username" => "root"
  "password" => null
  "database" => "clinic"
  "charset" => "utf8"
  "collation" => "utf8_unicode_ci"
  "prefix" => null
]

And I want to create a function to check if the submitted database credentials can make a connection to his database, so far I haven't found a way to do it by passing the credentials array. I've tried this way

if (DB::connection($request->validated())->getDatabaseName())
        {
            ray('The connection was successfull');
            return 'Connected to the DB: ' . DB::connection('myDamnDbConnection')-getDatabaseName();
        }

but it requires a string, not an array. Is there any way to do this?

1 Answer 1

3

Add the database configuration to your config as a new entry and then use that:

config([ 'database.connections.temp' => $request->validated() ]);
DB::connection('temp')->connect(); // Would throw an exception if it fails 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.