2017-02-15 1 views
0

SQLSTATE [42S02]: Basistabelle oder gefunden Ansicht nicht: 1146 Table 'patnership.zvss_new_transaction' existiert nicht (SQL: select * from zvss_new_transaction wo outletName = Uchumi und transactionDate = 2017-02- 13 Limit 1)Multidaten Verbindung mit PHP Laravel Rahmen

Ich bekomme diesen Fehler, wenn ich versuche, Multidatabase-Verbindung mit Laravel zu implementieren und es verbindet sich weiterhin mit der Standard-Datenbank auch nach Angabe der anderen Datenbanken in der Umgebung und Datenbankdateien. Ich habe auch die Verbindung auf meinem Modell hinzugefügt und trete weiter auf eine Wand ... Hilfe wird sehr geschätzt.

unter Befestigt sind die betreffenden Code

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, 
    ], 

], 

]; 

Modelldateien: home.php

<?php 

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

class Home extends Model 
{ 
    // 

    protected $connection = 'mysql2'; 

    protected $table='zvss_new_transaction'; 
} 

Controller: 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')); 
    } 

Antwort

0

konnte ich mein Problem mit folgenden Schritten

1.In Ihre .env lösen, fügen Sie eine weitere Datenbankverbindung params zB

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 

Die erste ist die übliche Verbindung, und die Die zweite ist die externe Datenbankverbindung.

2.In Ihre config/database.php unter Verbindung, fügen Sie den 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 dem Controller,

$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(); 

    } 

Diese wird verwendet, wenn Sie mit dem externen verbinden möchten Datenbank. Inhalieren Sie das Modell und geben Sie ihm die gewünschte Verbindung, und der Rest bleibt unverändert.