SQLSTATE[HY000] [1049] Unknown database 'laravel'
Asked Answered
C

7

7

I am getting this error while trying to save an object into DB.

SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: insert into cards (card_price, active, updated_at, created_at) values (0, 1, 2019-10-10 15:14:43, 2019-10-10 15:14:43))

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cardgame
DB_USERNAME=root
DB_PASSWORD=P@assword1!

Database.php

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_T_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

CardController.php

  public function generateCards()
  {
  $card = new Card();
  $card->card_price = 0;
  $card->active = 1;
  $card->save();
  }

Web.php

 Route::get('/generate-cards', 'CardController@generateCards');

Card.php

class Card extends Model
{
 protected  $guarded =[];
}

Migration file

public function up()
{
    Schema::create('cards', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('card_price');
        $table->integer('active');
        $table->timestamps();
    });
}

I've tried clearing cache and also have edited DB_PASSWORD To DB_T_PASSWORD as this corrected a similar issue earlier. Double checked the DB name, passwords etc & also other projects are already running also. I'm not able to figure it out.

Colloid answered 10/10, 2019 at 15:25 Comment(6)
show us the Card model and also run php artisan config:cache commandPhotoactinic
Have you tried to change database key by cardgame directly to Database.php file ?Isador
@Photoactinic php artisan config:cache did the job. Thank youColloid
@Isador No, but forge will anyway take the same value, right?Colloid
Glad i helped. Posted a full answer of the steps to take when you want to create a new database from scratch and migrate your tables. It could come handy for other users as well.Photoactinic
@Photoactinic Yes for sure! It helped!Colloid
P
13
  1. Go to your localhost server and drop the database and recreate a new one named cardgame

  2. Go to your laravel project in console and run php artisan config:cache command. That way all your env variables will be used.

  3. Run php artisan:migrate to run your migrations for your database and create your tables.

If you do the above 3 steps in that order you should be fine.

Photoactinic answered 10/10, 2019 at 15:37 Comment(0)
A
4

You must clear the cache because your old configuration is in the cache file, just run this command in your terminal for clear cache:

php artisan config:cache
Acidify answered 11/10, 2019 at 4:19 Comment(0)
S
0

You can solve this problem by 2 simple step.

Step:1 php artisan config:cache

step:2 php artisan migrate

Sibeal answered 15/8, 2020 at 14:1 Comment(0)
S
0

I had the same problem and I solved it following these steps:

1) Stop the server

2) Run php artisan config:clear .This command line will remove bootstrap\cache\config.php file

3) Start the server and It should work fine now

Soper answered 10/2, 2021 at 2:1 Comment(0)
M
0

create database in php admin then migrate your database again php artisan migrate , it's work 100%

Microscope answered 20/12, 2021 at 12:9 Comment(0)
H
0

I was also experiencing the same error and implemented all the solutions mentioned above but the error kept reoccuring. But I edited the.env and env.example file where the database = "laravel" to the name of my project for instance transport system and it worked. The best solution to the problem is editing both .env and env.example files with the name of your database that you created .

Helpmate answered 16/1, 2023 at 13:13 Comment(0)
C
0

you only need to config well your archive .env and create database right. After step is run the command 'php artisan migrate' to start migration on you database.

Colonist answered 15/6 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.