使用php laravel框架的多数据库连接

时间:2017-02-15 04:42:51

标签: php mysql laravel

SQLSTATE [42S02]:未找到基表或视图:1146表' patnership.zvss_new_transaction'不存在(SQL:select * from zvss_new_transaction其中outletName = uchumi和transactionDate = 2017-02-13 limit 1)

当我尝试使用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'));
    }

1 个答案:

答案 0 :(得分:0)

我能够通过以下步骤解决我的问题

1.在.env中,添加另一个数据库连接参数,例如

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.在你的config / database.php连接下,添加下面的

     '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.在你的控制器中,

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

     }

只要您想连接到外部数据库,就会使用它。对模型进行Intailize,并为其提供您想要使用的连接,然后其余部分保持不变。