2017-02-15 60 views
0

SQLSTATE [42S02]:未找到基本表或視圖:1146表'patnership.zvss_new_transaction'不存在(SQL:select * from zvss_new_transaction其中outletName = uchumi和transactionDate = 2017-02- 13限制1)多數據庫連接使用php laravel框架

當我嘗試使用laravel實現多數據庫連接時,我總是收到這個錯誤,並且即使在環境和數據庫文件中指定了其他數據庫後,它也會連接到默認數據庫。我還在我的模型中添加了連接,並繼續打牆...不勝感激。

下面附上有關代碼文件

database.php中

<?php 

return [ 

/* 
|-------------------------------------------------------------------------- 
| Default Database Connection Name 
|-------------------------------------------------------------------------- 
| 
| Here you may specify which of the database connections below you wish 
| to use as your default connection for all database work. Of course 
| you may use many connections at once using the Database library. 
| 
*/ 

'default' => env('DB_CONNECTION', 'mysql'), 

/* 
|-------------------------------------------------------------------------- 
| Database Connections 
|-------------------------------------------------------------------------- 
| 
| Here are each of the database connections setup for your application. 
| Of course, examples of configuring each database platform that is 
| supported by Laravel is shown below to make development simple. 
| 
| 
| All database work in Laravel is done through the PHP PDO facilities 
| so make sure you have the driver for your particular database of 
| choice installed on your machine before you begin development. 
| 
*/ 

'connections' => [ 

    'sqlite' => [ 
     'driver' => 'sqlite', 
     'database' => env('DB_DATABASE', database_path('database.sqlite')), 
     'prefix' => '', 
    ], 

    'mysql' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_HOST', 'localhost'), 
     'port' => env('DB_PORT', '3306'), 
     'database' => env('DB_DATABASE', 'patnership'), 
     'username' => env('DB_USERNAME', 'zippoco'), 
     'password' => env('DB_PASSWORD', ''), 
     'charset' => 'utf8mb4', 
     'collation' => 'utf8mb4_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 

    'mysql2' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_HOST', 'hostname'), 
     'port' => env('DB_PORT', '3306'), 
     'database' => env('DB_DATABASE', 'databasename'), 
     'username' => env('DB_USERNAME', 'username'), 
     'password' => env('DB_PASSWORD', 'password'), 
     'charset' => 'utf8mb4', 
     'collation' => 'utf8mb4_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 

    'pgsql' => [ 
     'driver' => 'pgsql', 
     'host' => env('DB_HOST', '127.0.0.1'), 
     'port' => env('DB_PORT', '5432'), 
     'database' => env('DB_DATABASE', 'forge'), 
     'username' => env('DB_USERNAME', 'forge'), 
     'password' => env('DB_PASSWORD', ''), 
     'charset' => 'utf8', 
     'prefix' => '', 
     'schema' => 'public', 
     'sslmode' => 'prefer', 
    ], 

], 

/* 
|-------------------------------------------------------------------------- 
| Migration Repository Table 
|-------------------------------------------------------------------------- 
| 
| This table keeps track of all the migrations that have already run for 
| your application. Using this information, we can determine which of 
| the migrations on disk haven't actually been run in the database. 
| 
*/ 

'migrations' => 'migrations', 

/* 
|-------------------------------------------------------------------------- 
| Redis Databases 
|-------------------------------------------------------------------------- 
| 
| Redis is an open source, fast, and advanced key-value store that also 
| provides a richer set of commands than a typical key-value systems 
| such as APC or Memcached. Laravel makes it easy to dig right in. 
| 
*/ 

'redis' => [ 

    'client' => 'predis', 

    'default' => [ 
     'host' => env('REDIS_HOST', '127.0.0.1'), 
     'password' => env('REDIS_PASSWORD', null), 
     'port' => env('REDIS_PORT', 6379), 
     'database' => 0, 
    ], 

], 

]; 

型號:Home.php

<?php 

namespace App; 
use Illuminate\Database\Eloquent\Model; 

class Home extends Model 
{ 
    // 

    protected $connection = 'mysql2'; 

    protected $table='zvss_new_transaction'; 
} 

控制器:HomeController.php

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Home; 

class HomeController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     // 


     $data=Home::where('outletName', '=', "uchumi") 
      ->where('transactionDate', '=','2017-02-13') 
      ->first(); 

     return view('home',compact('data')); 
    } 

回答

0

我能解決我的問題有以下步驟

1.In您.ENV,添加其他數據庫連接PARAMS如

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_DATABASE=database1 
DB_USERNAME=username1 
DB_PASSWORD=password1 
DB_HOST=host1 

DB_EXT_CONNECTION=mysql 
DB_EXT_DATABASE=database2 
DB_EXT_USERNAME=username2 
DB_EXT_PASSWORD=password2 
DB_EXT_HOST=host2 

第一種是平時的連接,並且第二個是外部數據庫連接。

2.In你的config /下連接database.php中,添加fowllowing

 'mysql2' => [ 
     'driver' => 'mysql', 
     'host'  => env('DB_EXT_HOST', 'host2'), 
     'database' => env('DB_EXT_DATABASE', 'database2'), 
     'username' => env('DB_EXT_USERNAME', 'username2'), 
     'password' => env('DB_EXT_PASSWORD', 'password2'), 
     'charset' => 'utf8', 
     'collation' => 'utf8_unicode_ci', 
     'prefix' => '', 
     'strict' => false, 
    ], 
    //Now we have established the connection, 

3.In控制器,只要你想連接到外部

$someModel = new Models(); 

    $someModel->setConnection('mysql2'); 
    $something = $someModel->get(); 


    foreach($something as $mod){ 
     $model=new Models(); 
     $model->id=$mod->id; 
     $model->make_id=$mod->make_id; 
     $model->make=$mod->make; 
     $model->model=$mod->model; 
     $model->save(); 

    } 

這是用來數據庫。把模型弄清楚,給它連接你想使用的,然後其餘的保持不變。