Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing database name in Laravel/Homestead

I started learning Laravel just an hour ago and following tutorials.

My settings for database are as follow (File: app/config/database.php):

'default' => 'mysql',

'connections' => array(

    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),

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

In mySQL on homestead, I already created laraveltest database. I also created migration file in database/migration directory with following code:

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('name');
        $table->timestamps();
    });
}

and migration commands were:

vagrant@homestead:~/Code/Laravel$ php artisan migrate
Migration table created successfully.
Migrated: 2014_08_14_195103_create_users_table

As displayed, table users created but in homestead database, not in laraveltest database. Can someone please tell where I went wrong and how to force laravel to use laraveltest database?

Edit after first comment File: bootstrap/start.php

$env = $app->detectEnvironment(array(

    'local' => array('homestead'),

));
like image 377
Kapil Sharma Avatar asked Aug 14 '14 20:08

Kapil Sharma


People also ask

How do I change the default database in Laravel?

To change its default database type, edit the file config/database. php: Search for 'default' =>env('DB_CONNECTION', 'mysql') Change it to whatever required like 'default' =>env('DB_CONNECTION', 'sqlite')

How do I access my homestead database?

A homestead database is configured for both MySQL and PostgreSQL out of the box. To connect to your MySQL or PostgreSQL database from your host machine's database client, you should connect to 127.0. 0.1 and port 33060 (MySQL) or 54320 (PostgreSQL). The username and password for both databases is homestead / secret.

How do I access MySQL in Homestead?

To connect to your MySQL or Postgres database from your main machine via Navicat or Sequel Pro, you should connect to 127.0. 0.1 and port 33060 (MySQL) or 54320 (Postgres). The username and password for both databases is homestead / secret .


2 Answers

It is most likely an issue with environments. Check that you have a database configuration file in app/config/local/, and check that it has the proper settings.

like image 108
camroncade Avatar answered Sep 22 '22 14:09

camroncade


If anyone finds this looking for the answer for Laravel 5 or later: It's the same as @camroncade says, but in this case it's your .env file in the project root you want to look at.

like image 31
Karin van den Berg Avatar answered Sep 23 '22 14:09

Karin van den Berg