How to create new empty database with Laravel over Model

laravel create database
laravel database
laravel create database dynamically
laravel create database if not exists
laravel database transactions
how to connect database in laravel 7
laravel 6 db
best database for laravel

is there any way to create new database with Laravel CodeBehind ? I mean, I'm not able to use php artisan or something like that. Just because I want to create a database for every registered user. After that I can use migration. I'm using Laravel 5.2. Thanks for your support already.

well, you could perform a database query for each bd and than use the mysql or you sgbd database create command, for eg;

  DB::getConnection()->statement('CREATE DATABASE :schema', array('schema' => "dasdsdasdsadsadsa"))

It probably will work with what you want.

Migrations & Seeding - Laravel, Tables. Creating Tables. To create a new database table, use the create method on the Schema facade. The create� The retrieved event will fire when an existing model is retrieved from the database. When a new model is saved for the first time, the creating and created events will fire. The updating / updated events will fire when an existing model is modified and the save method is called.

You can run artisan commands from code.


To run composer commands (you'll need it to register migrations), use php shell_exec:

shell_exec('composer dumpauto');

Database: Migrations - Laravel, You may specify a custom table by defining a table property on your model: Once you have created a model and its associated database table, you are ready to mass assignable, you may define the $guarded property as an empty array: The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Generating Migrations. To create a migration, use the make:migration Artisan command: php artisan make:migration create_users_table. The new migration will be placed in your database/migrations

You can create a database using DB facade like this (provided the sql user has the rights to do that):

DB::statement('CREATE DATABASE myNewDB');

But I would urge you to reconsider your data structures - having a separate database for every user is most probably not the right solution to whatever problem you are having and could create more issues than it would solve.

Eloquent: Getting Started - Laravel, To create a new database table, the Schema::create method is used: To drop a column, you may use the dropColumn method on the Schema builder. Before� How to connect Mysql Database with Laravel. How to Configure database connection in Laravel 5. How to create Mysql table from Laravel application. How to make Database connection in Laravel and

You can create as many Databases from controller by passing the dbname like this

public function index() {
    $userName = 'bigboytr';  // Your Database name to be created
    DB::statement("CREATE DATABASE $userName");

Warning :

You don't need to create one database for every user as it will make the system very complex. Ex., If you have some thousands of user in some case, then moving the user's information to registered users 'general' database would be very tough

Recommendation :

What i suggest you it to create a new table for unregistered user, so that you can move them to registered user's table, which would make your process easy.

Tip :

You can compare the Database Engine and choose the best for your match like this

Schema Builder - Laravel, an issue where Laravel refuses to insert data when using the Model::create() data on that model, it's just the create method that doesn't seem to insert anything . above code create an entry in the db with literally nothing in it accept an id? For convenience, Laravel supports these URLs as an alternative to configuring your database with multiple configuration options. If the url (or corresponding DATABASE_URL environment variable) configuration option is present, it will be used to extract the database connection and credential information.

for this, you can create an Artisan Command

Step:1 Create command

php artisan make:command CreateDatabaseCommand

Step:2 In app/Console/Kernel.php register the command

protected $commands = [

Step:3 Write logic in your CreateDatabaseCommand.php file

protected $signature = 'make:database {dbname} {connection?}';

    public function handle()
         $dbname = $this->argument('dbname');
         $connection = $this->hasArgument('connection') && $this->argument('connection') ? $this->argument('connection'): DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME);

         $hasDb = DB::connection($connection)->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "."'".$dbname."'");

         if(empty($hasDb)) {
             DB::connection($connection)->select('CREATE DATABASE '. $dbname);
             $this->info("Database '$dbname' created for '$connection' connection");
         else {
             $this->info("Database $dbname already exists for $connection connection");
     catch (\Exception $e){

Now your ready Artisan command call like this way

php artisan make:database {database-name} {connection-name}

See my sample

Eloquent Create Method - Always inserts blank entries., When you try to create a blank model, I get an error like this: QueryException in When you do Model::create() it creates a new instance and tries to store it in the DB. @noeldiaz I think laravel automatically escape the names. Nine out of ten doctors recommend Laracasts over competing brands. Create migrations and models from existing database Posted 4 years ago by rabol Hi Problem: I have an old 'plain' PHP website that I would like to migrate to Laravel So I would like to create migrations and Models for all the tables in the current database

I can not create an empty model, Before creating migrations, We will need to setup our database, In laravel, the name of the model has to be singular and the name of the need to do in this method is fetch the specified Todo and call delete() method on it. Type the following command to create a model and migration files. php artisan make:model Show -m. In laravel, the name of a model has to be singular, and the name of the migration should be the plural so it can automatically find a table name. You can find these migration files inside the database/migrations directory.

Laravel 5.8 From Scratch: Config, ENV, Migrations, and Todos , Creating a database on the fly is actually fairly simple using Laravel. The easiest approach is to create a new base model to use for all� Using an Artisan command to create a database in Laravel or Lumen. We’re heavy users of Laravel and Lumen here at EvaluAgent, and we needed a way to automatically create the database for each of our services when we deployed them. A quick Google search provides mixed results, so I thought I’d blog our solution in an effort to help others.

Create new database and tables on the fly, Inserting is a simple database action that makes an sql statement and puts data directly into the table. Creating is about instantiating a new Eloquent Model. Eloquent provides convenient methods for adding new models to relationships. For example, perhaps you need to insert a new Comment for a Post model. Instead of manually setting the post_id attribute on the Comment, you may insert the Comment directly from the relationship's save method:

  • well, so, for each user in some table, you wanna create a database for it?
  • Yes, I have general db for store registered users. But I need to new db for every user.
  • This kind of solution is called "multi-tenant", here is an article that describe it Multi-Tenant Data Architecture
  • @ArieCwHat Thanks for the document. I read that before. But I change my project to as a service model.
  • Yes it's simple and solved my problem. Thanks to you and everyone to answered or commented my question.
  • I'll try this but not now.
  • Thanks for your advice and I agree you, it's a bit complicated. But we have to create DB for every user. Actually user is not user. User is a company. So companies has lot of data. I can't keep them in one DB. By the way, it's a question. I'm newbie on laravel. Where I'll run this code. It will controller or model ?
  • Thats up to you to decide. Generally you would not want to put too much logic into laravel controllers because code in controllers cannot be reused in a nice way, but if this database creation happens only in one place then you could get away with that. Otherwise I would try to go with object-oriented approach and do database creation as a method in an appropriate entity model, such as $company->createDatabase()