php - laravel Database [postgres] not configured.
本文介绍了php - laravel Database [postgres] not configured.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
Database [postgres] not configured.
以上错误是通过model连接其它数据库时报的错误
数据库配置(config/database.php)里明明是有的,使用的是默认配置(.env文件不会用到)
请各位大神帮忙解决下,谢谢!
以下是model层代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class StaKeyModel extends Model
{
protected $guarded = [];
protected $connection = 'postgres';
protected $table = 'sta_key';
/**
* @date 2017-07-17 16:05:39
*
* @param $startDate
* @param $endDate
* @return mixed
*/
public static function getList($startDate, $endDate) {
$object = static::select(DB::raw('date,key,count(distinct(value)) v'));
$object->where('date', '>=', $startDate);
$object->where('date', '<=', $endDate);
$object->groupBy('date');
$object->groupBy('key');
$object->orderBy('v', 'desc');
$data = $object->get()->toArray();
return $data;
}
}
以下是: database.php 文件配置(部分默认省略)
'default' => env('DB_CONNECTION', 'mysql');
'connections' => [
//支付数据库
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'mysqldb'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'postgres' => [
'driver' => 'postgres',
'host' => env('DB_HOST_PGSQL', 'localhost'),
'port' => env('DB_PORT_PGSQL', 5439),
'database' => env('DB_DATABASE_PGSQL', 'postgredb'),
'username' => env('DB_USERNAME_PGSQL', 'user'),
'password' => env('DB_PASSWORD_PGSQL', 'password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
];
解决方案
问题已找到
主要原因是在修改配置之前在项目下执行了如下命令:
php artisan config:cache
执行结果:
Configuration cache cleared!
Configuration cached successfully!
目的是为了将所有的配置缓存到一个配置文件里,文件路径如下:
因此在使用该缓存配置的情况下,需要在修改配置之后重新执行命令:php artisan config:cache
注意:该缓存是针对所有的配置
这篇关于php - laravel Database [postgres] not configured.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文