SQLSTATE[42S02]:找不到基表或视图:1146 表 'patnership.zvss_new_transaction' 不存在(SQL:从zvss_new_transaction
中选择 *,其中 outletName
= uchumi 和 transactionDate
= 2017-02-13 限制 1(
当我尝试使用 laravel 实现多数据库连接时,我不断收到该错误,即使在环境和数据库文件中指定了其他 datbase 之后,它也会继续连接到默认数据库。我还在我的模型上添加了连接并继续撞墙......协助将不胜感激。
下面附有相关的代码文件
数据库.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,
],
],
];
型号:首页.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Home extends Model
{
//
protected $connection = 'mysql2';
protected $table='zvss_new_transaction';
}
控制器:首页控制器.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHome;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
//
$data=Home::where('outletName', '=', "uchumi")
->where('transactionDate', '=','2017-02-13')
->first();
return view('home',compact('data'));
}
我能够通过以下步骤解决我的问题
1.In 您的 .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.In 配置/数据库.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.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();
}
每当要连接到外部数据库时,都会使用此方法。给模型尾部,并给它你想要使用的连接,然后其余的保持不变。