如何在heroku的laravel项目中添加另一个数据库

时间:2019-07-17 08:07:00

标签: mysql laravel postgresql heroku

我将我的laravel项目部署在heroku中,但是我仅迁移了我创建的数据库,例如:用户,新闻。但是我有另一个数据库未从我的laravel项目迁移。意味着,这是一个现有的数据库,我仅将其连接到我的项目中。在开发阶段,我可以使用下面的代码连接第二个数据库。但是现在,我将项目部署在heroku中,但我不知道如何连接第二个数据库,因为在heroku的postgresSQL中,您只能基于laravel中的迁移文件夹来创建和迁移数据库。我不知道如何在heroku的postgresSql中上传sql文件,这样我可以使用下面的代码连接第二个数据库。在Heroku中这可能吗?因为第二个数据库在我的登录页面中很重要。它包含一些select查询。

这是我的一些代码,包括第二个数据库的连接。

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HoQcNyCc5KEGw4yjqpBIdKzTC+yeDoOJcerVMEVx+fs=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=adminpanel
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=ricjac8_orocoin
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

database.php


    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        '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_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'),
            ]) : [],
        ],

        'mysql2' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST_SECOND'),
            'port'      => env('DB_PORT_SECOND'),
            'database'  => env('DB_DATABASE_SECOND'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD','forge'),
            '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'),
            ]) : [],
        ],

GraphController.php-具有从我的其他数据库中进行选择查询的那个

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;
use View;
use App\News;
use Charts;
use App\Graph;
use App\Roadmap;
class GraphController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
        $allroad = Roadmap::all();
        $news = News::all();

        //the second database query
        $graphdetails = Graph::select()->where('id', 1)->get();
        return view('coin.news',compact('news','graphdetails','allroad'));
    }

}

graph.blade.php-选择查询在图表中呈现的结果

//other blade codes i didint include here.


@foreach ($graphdetails as $item)
@endforeach


 chart.legend = new am4charts.Legend();
    chart.data = [{
      "tokens": "Sold Tokens",
      "values": {{$item->total_tokens}} - {{$item->sales_token}}
    },{
      "tokens": "Unsold Tokens",
      "values": {{$item->sales_token}}
    }];
  });

Graph.php模型

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
use Cache;
use Charts;
class Graph extends Model
{
    //
    protected $connection = 'mysql2';
    protected $table = 'ico_stages';
    protected $fillable = [
        'name', 'start_date', 'end_date', 'total_tokens', 'base_price', 'min_purchase', 'max_purchase', 'soft_cap', 'hard_cap',
        'display_mode','private','user_panel_display','sales_token','sales_amount','status',
    ];
}

1 个答案:

答案 0 :(得分:2)

尝试使用MySQL的另一个免费共享主机。 PostreSQL与MySQL有点不同。