Home » Php » Can't Figure out how to set up a database on Laravel

Can't Figure out how to set up a database on Laravel

Posted by: admin November 29, 2017 Leave a comment

Questions:

This is becoming a really annoying problem. I am following the Quick setup directions on laravel.com. I have followed all the steps until I have to go:

php artisan migrate

The result is:

[PDOException]
  SQLSTATE[42000] [1049] Unknown database 'database'



migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]

And I go back and check the database.php file and it reads:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

Afterwards, I go to localhost:8000/users and it displays:

    PDOException
    SQLSTATE[42000] [1049] Unknown database 'database'
    open: /Users/benamorgan/Sites/run-away-world/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
         */


    public function createConnection($dsn, array $config, array $options)
        {
            $username = array_get($config, 'username');

            $password = array_get($config, 'password');

            return new PDO($dsn, $username, $password, $options);
        }

I have been trying to change the database name, provide a root password, start and stop mysql, change the naming in every possible way for database, change localhost to 127.0.0.1. I don’t know what to do, hence I’m stopping by here to ask where I messed up.

Answers:

Laravel doesn’t create databases for you, you need to do it on your own. Log in to mysql and create your database, then provide the name in your db config.

You can do it quickly in your console:

echo "create database my_database" | mysql -u root -p

then in your config change the database name:

'database'  => 'my_database',

Questions:
Answers:

To add, make sure that you are using the MySQL driver in the config file, although this must be obvious.

again make sure the database in MySQL is created prior and you are using the right credentials (User, Key)