Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vm's avatar
Level 2

forge not migrating database

In development my bootstrap /start.php looked like

$env = $app->detectEnvironment(array(

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

));

after watching laracast segment on Environment i changed it to

$env = $app->detectEnvironment(function()
{
    return getenv('ENV') ? : 'local';
});

On forge i have server installed and uploading from github. During development I used a different name than forge for my database. So when I set up the server on forge I used the same database name rather than using the default 'forge' name for database. However when i ssh into my server I can see the database with the same name as my development database but none of the tables are there. My save deploy script on forge is the default like so

cd /home/forge/default
git pull origin master
composer install
php artisan migrate --force

my config/database.php has the following

'default' => 'mysql',
....

connections' => array(

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

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

I have also added above 4 environment variables on the server. I also see them in the .env.php file on the server

What am I doing wrong? Why are my database migrations not on the server ? Hope someone can help! Thx

I see this on a previous request titled "I can't get Forge to work " answered by @Cocoon suggesting to put the following in start.php
As in the case of that request, my latest deployment log has

[PDOException]
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

$env = $app->detectEnvironment(function () {
$environment = getenv('ENV');
if (strlen($environment) == 0) {
if (strpos(__DIR__, '/home/forge/dev.example.com') === 0) {
$environment = 'staging';
} elseif (strpos(__DIR__, '/home/forge/example.com') === 0) {
$environment = 'production';
}
}
if (strlen($environment) == 0) {
$environment = 'local';
}
return $environment;
});

But I don't get it. What's with example.com. Can someone explain

0 likes
3 replies
vm's avatar
vm
OP
Best Answer
Level 2

For anybody reading this in the future what saved my day was a post I found on Mat Stauffers blog and it works for me. So in my start.php I put the following and the db is migrated.

$env = $app->detectEnvironment(array(
    'production' => array('your-forge-staging-server-host-name-here'),
    'local' => array('homestead', '.local')
));
almazik's avatar

@vichu I just faced that same issue and yes this solution works, indeed. But, what if I want to go with return getenv('ENV') ? : 'local'; approach? Didn't you happen to find out how can we allow for that? Because Jeff in his tutorial was able to and that's weird

vm's avatar
Level 2

@andrew.malinnikow: Exactly, that is why I posted the request. My take is that Jeff is setting the value for 'production' else where as default in his server setup.

Please or to participate in this conversation.