You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中Artisan::call方法--database参数失效问题求助

Troubleshooting "Database [new_database] not configured" Error Even After Configuring in database.php

Hey there! Let’s walk through why you’re seeing that frustrating InvalidArgumentException even after adding the new_database connection to your database.php file. I’ve dealt with this exact issue a few times, so here are the most likely fixes to check step by step:

  • Clear Your Configuration Cache First
    Laravel caches configuration files to boost performance, which means changes to database.php won’t take effect until you clear this cache. Run these commands in your terminal:

    php artisan config:clear
    php artisan cache:clear
    

    If you’re working in a production environment, make sure you don’t have any persistent caching layers (like Redis or APC) that might still hold old config values.

  • Double-Check Your Connection’s Configuration Syntax
    It’s easy to miss a comma, misspell a key, or leave out a required parameter in your database.php connections array. Verify that your new_database entry looks something like this (adjust for your database driver):

    'new_database' => [
        'driver' => 'mysql',
        'host' => env('DB_NEW_HOST', '127.0.0.1'),
        'port' => env('DB_NEW_PORT', '3306'),
        'database' => env('DB_NEW_DATABASE', 'new_db'),
        'username' => env('DB_NEW_USERNAME', 'root'),
        'password' => env('DB_NEW_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
    ],
    

    Pay close attention to the connection name (new_database)—it’s case-sensitive, so make sure you’re using the exact same string when referencing it in your migration commands or code.

  • Make Sure Dynamic Setup Happens Before Migrations Run
    If you’re dynamically adding or modifying the new_database connection (instead of hardcoding it in database.php), timing is everything. If your dynamic config code runs after Laravel loads its configuration (like after the migration command boots), the system won’t see the new connection.

    If you’re setting the connection dynamically in code, add this after updating the config to refresh the database manager:

    use Illuminate\Support\Facades\Config;
    use Illuminate\Support\Facades\DB;
    
    // Update the connection config
    Config::set('database.connections.new_database', [
        // Your dynamic config here
    ]);
    
    // Purge the old connection and reconnect
    DB::purge('new_database');
    DB::reconnect('new_database');
    

    For Artisan commands, ensure your dynamic config runs before the migration command executes—you might need to create a custom command that sets up the connection first, then calls the migrate command.

  • Check for .env File Issues
    If your database.php uses env() variables (like in the example above), confirm that your .env file has all the required entries for new_database. A missing or misspelled variable (e.g., DB_NEW_DATABSE instead of DB_NEW_DATABASE) will cause the connection to fail silently, making it look like the connection isn’t configured at all.

  • Verify You’re Targeting the Correct Connection in Migrations
    When running migrations, make sure you’re explicitly specifying the new_database connection with the --database flag:

    php artisan migrate --database=new_database
    

    If you forget this flag, Laravel will use your default database connection, but the error you’re seeing suggests you are specifying it—just double-check the spelling matches exactly what’s in database.php.

Start with clearing the config cache (that’s the #1 culprit most of the time) and work your way through these checks. Chances are one of these will resolve the issue!

内容的提问来源于stack exchange,提问作者GRESPL Nagpur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:53:10