Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Roni's avatar
Level 33

Creating a database with php artisan

Had a bit of spare time to tinker today and was working on a throw away project remotely and for the first time I ran into this issue. Can I create the database I'm working on with a laravel artisan command?

For example once you have your app/config/database.php and your app/config/local/database.php set up. is there a starting point within laravel or do you always need to make the database separately outside of the project.

That part seems to be skipped in all the documentation, and I do tend to just premake the db when I make the project. So I've never run into it before. But it seems like I'm missing a first step.

Cheers

0 likes
11 replies
Roni's avatar
Level 33

Thanks, I hadn't seen that series yet,

OUCH! I hate to take back an award, and I'm not sure I can, however, that did not address the question. I thought I had missed something in the generators package but that's not the the case.

None of these generators "CREATE THE DATABASE". If anyone knows a laravel style way to do this please let me know. If it's just something that has to be done manually thats fine too. It would be nice when working in a team since we control the environment anyhow with homestead if I could take all the manual effort out of the picture.

Cheers

bestmomo's avatar
Level 52

You must create an artisan command for that with a raw query :

DB::getConnection()->statement('CREATE DATABASE :schema', ['schema' => $schemaName]);

The schemaName could be get from config.

3 likes
Roni's avatar
Level 33

thanks @bestmomo, I'll check out the command series and I'll try and do that and post it here once I figure out how to do that.

Cheers

warksit's avatar

You can now configure the databases you want in your homestead.yaml file.

1 like
Roni's avatar
Level 33

@warksit, I believe so, but you need to restart your vm with the --provision field. There may be times at some of the places I consult where I'm not able to do that. Some companies have dev.example.com running along side their production example.com for multiple people and I just have to fit in the workflow. Many times our first languages are lost in translation. For example, I do some work in Montreal, primarily with french speaking network guys and my french is well... weak. So what I hear might not be what was said, and vice versa.

I'm not saying the world should work this way, but it seems laravel by convention uses artisan commands for all this generation, and logical that one might want to have this preliminary step.

Truthfully I have no idea why it hasn't been a part of the commands available, I would imagine that it makes sense for this to be part of the regular migration command set. But again I'm new to laravel, I've never deployed production servers with laravel and certainly haven't contributed to it, so there may be a really good reason why it's not part of the workflow. I'm just making sure it's part of MY workflow.

I've suffered enough lost time to simple typo's over the years that anything I CAN automate, I just do.

vkovic's avatar

I just created an interesting Laravel package that has some handy set of artisan commands. Beside the discussed one, to create database, it supports deleting database, dumping to .sql and loading dump from it and some more ...

Check it out: vkovic/laravel-commando

1 like
nategg's avatar

I'm brand new to Laravel (less than a week) and only learning web dev a couple years, so I don't have a good handle on Artisan commands. I see the original question was posed FIVE YEARS AGO??! There's still a million things I don't understand in programming/Laravel, but (being more civil than I'm accustomed) WHY ON EARTH is there no way to create the database directly in Laravel thru an IDE? Is it not possible? What am I missing?

dillonkavanagh's avatar

Incase anyone is looking, it's very simple in most situations. $this->database is my DB name,

use Illuminate\Support\Facades\DB;

// Connection name = `mysql` for me, adjust as suits for you
DB::connection('mysql')->statement("CREATE DATABASE ?", [$this->database]);
// Simply written
DB::connection('mysql')->statement("CREATE DATABASE my_new_db");
1 like

Please or to participate in this conversation.