0

I have used symfony2 console to create database. If I want to create a database named "symfony" I usually mentioned that name in parameters.yml file and run the below command in console

php app/console  doctrine:database:create

But when came to laravel, I don't find similar command to create database in laravel. Can anyone help me to find out those command to create database directly from Laravel Console.

3 Answers 3

2

You can do that but you will have to create your own command.

First, run php artisan command:make CreateDatabase --command=database:create to generate app/commands/CreateDatabase.php

Then open that file and change it to this: (I left out all comments, but you can obviously keep them in there)

class CreateDatabase extends Command {
    protected $name = 'database:create';
    protected $description = 'Command description.';

    public function fire()
    {
        DB::statement('CREATE DATABASE '.$this->argument('name'));
    }

    protected function getArguments()
    {
        return array(
            array('name', InputArgument::REQUIRED, 'Database name'),
        );
    }
}

Now you only have to register the command in app/start/artisan.php:

Artisan::add(new CreateDatabase);

and you're good to go.

That's how you call it:

php artisan database:create your-desired-database-name

Alternative: artisan tinker

You can always use php artisan tinker to run PHP code (with Laravel bootstrapped):

php artisan tinker
> DB::statement('CREATE DATABASE your-desired-database-name');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your nice answer. If we see symfony2 command then saw, it first reads parameters.yml file and pick database name and create new database in system. I think slide modification may achieve my target.
You could create create your own config file in app/config and use Config::get() to retrieve the parameters. Let me know if you need more help with it...
0

As far as I know, you can use php artisan migrate to make migrations including creating tables from Laravel Console. However, you need to create and modify migration files first, where you can create or drop tables.

So if you want to create a table directly from Laravel Console using something like php artisan create database table ***, it is not possible.

Comments

0

I think you can not create database through command so go to app/config/database.php and set your database configuration. details here

'mysql' => array(
    'read' => array(
        'host' => '192.168.1.1',
    ),
    'write' => array(
        'host' => '196.168.1.2'
    ),
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

after setting you can create table through command

php artisan migrate:make create_user_table --table=users

it will generate a file to app/database/migrations name create_users_table. . . . . then you create your table following this link

and finally run this command php artisan migrate

2 Comments

Following command just create a table in database. In here, I have to create database manually but I don't want to create it manually. Just write my command in laravel console and system will create my database automatically.
No laravel doesn't allow to create database through command line so you need to create database manually.

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.