-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Database using DB::statement doesn't works. #19412
Comments
That's it, that's how the query builder is supposed to work, it needs to connect to a DB before running queries. |
Doesn't particularly answer your question, but may help:
You could use the My use-case is a little different - it creates databases when scaffolding new projects via artisan command. But it means I don't need to create any new PDO objects or hard-code DB information. I'm not sure if you can jump right into using DB functions from here, or whether you'd need to 'reset' any of the DB instance objects. Hopefully this is somewhat helpful? |
@themsaid I thought that the statement
So, selecting the connection i can use the
Sorry :P PS: When the
|
For pgsql:
|
I found it best to use the Laravel config objects directly so you pick up any valid Laravel configuration values, not just env vars (like values specified in database.php). To use the DB::statement command, you have to dynamically set the database config value to null. Here is the flow: I set the database to null, executed the statement, then set it back and purged the database-less connection from the cache (so the next connection will use the newly created database). // Get the default connection name, and the database name for that connection from laravel config.
$connectionName = config('database.default');
$databaseName = config("database.connections.{$connectionName}.database");
// Set the database name to null so DB commands connect to raw mysql, not a database.
config(["database.connections.{$connectionName}.database" => null]);
// Create the db if it doesn't exist.
DB::statement("CREATE DATABASE IF NOT EXISTS " . $databaseName);
// Reset database name and purge database-less connection from cache.
config(["database.connections.{$connectionName}.database" => $databaseName ]);
DB::purge(); |
my working example based on the answers of all of you: create new artisan command: the content of App\Console\Commands\mysql.php :
then run: (schema_name is optionally) |
Hey Folks,
Cheers! |
whenever I'm running something I get the error: Can this be some kind of middleware that tries to initialise DB before the handle method even gets invoked? |
5.4.17
PHP 5.6.30-0+deb8u1 (cli) (built: Feb 8 2017 08:50:21)
CLIENT
mysql Ver 8.0.1-dmr for Linux on x86_64 (MySQL Community Server (GPL))
SERVER
Server version: 8.0.1-dmr MySQL Community Server (GPL)
PDO MYSQL:
Client API version => 5.5.55
Description:
When i tried to do a statement query to create a new database it gives me this error.
I do this in the
console.php
file that defines artisan commands.Steps To Reproduce:
This is the code that (tries to) create my database:
Also I have tried these expression:
But nothing works :/
So, finally I made this:
I think that this problem occurs because the connection tries to connect to the default database (setted in the configuration), so i can't do any query if the database doesn't exists. A way to check that is catching the exception generated when the statement is executed. Also, in the documentation nothing indicates how to do this without auto-select the database of the connection.
The text was updated successfully, but these errors were encountered: