I am getting very strange results with Laravel 5, a custom database config, and MAMP

362 views Asked by At

I am using Laravel 5 with a .env and also a config/database.php file that reads as follows:

'mysql' => array(
    'default' => 'mysql',
    'driver' => 'mysql',
    'unix_socket' => getenv('UNIX_SOCKET'),
    'host' => getenv('DB_HOST'),
    'database' => getenv('DB_DATABASE'),
    'username' => getenv('DB_USERNAME'),
    'password' => getenv('DB_PASSWORD'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => ''),

php artisan migrate fails when I run it, claiming that the Database is not configured. However, I suspect I am doing something wrong in my config file because when I run php artisan -V I get this?

>php artisan -V
'mysql' => array(
    'default' => 'mysql',
    'driver' => 'mysql',
    'unix_socket' => getenv('UNIX_SOCKET'),
    'host' => getenv('DB_HOST'),
    'database' => getenv('DB_DATABASE'),
    'username' => getenv('DB_USERNAME'),
    'password' => getenv('DB_PASSWORD'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => ''
),Laravel Framework version Lumen (5.1.1) (Laravel Components 5.1.*)

php artisan appears to be just printing out my config file and is ignoring it?

1

There are 1 answers

5
David Barker On BEST ANSWER

Laravel and Lumen config files need to be structured properly for them to work. Also getenv() is not the function you want, instead you need env(). The database config needs to be structured like this (just swap out your variables):

<?php

return [

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => env('DB_PREFIX', ''),
            'timezone'  => env('DB_TIMEZONE','+00:00'),
            'strict'    => false,
        ]
    ]
];

Here's a the full Lumen database config file for your reference.