Database [] not configured Laravel 5

前端 未结 6 905
闹比i
闹比i 2021-01-08 00:59

I create new db in phpmyadmin and new tables.

Then i do

    public function next(Request $request){
    $langs = DB::connection(\'mydb\')->select(         


        
相关标签:
6条回答
  • 2021-01-08 01:34

    You're using single connection, so you don't need to specify it:

    $langs = DB::table('lang')->get();
    

    You should use connection() method only when you're working with multiple DB connections.

    0 讨论(0)
  • 2021-01-08 01:43

    I struggled with this error for a few hours and found out solution that works for me. If you can not delete cache with php artisan config:cache because it throws error after you run it:

    [InvalidArgumentException]

    Database [] not configured

    And if you are sure that your connections parameters are good then try manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder. Hope it helps someone.

    0 讨论(0)
  • 2021-01-08 01:49

    In my case it was a bad database username. I had it set in my config/database.php as well as the .env file and one of them was different than the other. Found this thread when searching, thought this might help someone.

    0 讨论(0)
  • 2021-01-08 01:52

    In my case I used 2 db connections and the second added was not cached, the command call: php artisan config:cache did the trick.

    To see what's going on it was sufficient to output the $connections variable in DatabaseManager->configure method.

    0 讨论(0)
  • 2021-01-08 01:53

    Anyone stumbling upon this - if you are using Laravel 5.4 or newer, you can setup a new database connection in config/database.php

        'mysql_test' => [
            'driver'      => 'mysql',
            'host'        => env('DB_HOST', '127.0.0.1'),
            'port'        => env('DB_PORT', '3306'),
            'database'    => env('DB_DATABASE_TEST', 'forge'),
            'username'    => env('DB_USERNAME_TEST', 'forge'),
            'password'    => env('DB_PASSWORD_TEST', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset'     => 'utf8mb4',
            'collation'   => 'utf8mb4_unicode_ci',
            'prefix'      => '',
            'strict'      => false,
            'modes'       => [
                'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_AUTO_CREATE_USER',
                'NO_ENGINE_SUBSTITUTION',
            ],
            'engine' => null,
        ],
    

    I created 3 new env variables DB_USERNAME_TEST, DB_PASSWORD_TEST, DB_DATABASE_TEST

    Edit .env with something like

    DB_DATABASE_TEST=test_db
    DB_USERNAME_TEST=local
    DB_PASSWORD_TEST=localpassword
    

    Make sure config is refreshed: php artisan config:cache

    You should be able to run a database migrate on your new test database, like so:

    php artisan migrate:refresh --database=mysql_test

    0 讨论(0)
  • 2021-01-08 01:56

    For me the connection worked in development but not in the production environment, so after a lot of searching, I had to rebuild the configuration cache and It has worked. I ran the following command in the laravel root directory:

    php artisan config:cache 
    
    0 讨论(0)
提交回复
热议问题