php1049,SQLSTATE[HY000] [1049] Unknown database 'laravel'

原文:

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.

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Go to your localhost server and drop the database and recreate a new one named cardgame

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

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.

# Answer 2

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

# Answer 3

You can solve this problem by 2 simple step.

Step:1 php artisan config:cache

step:2 php artisan migrate

# Answer 4

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值