gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:24:37 25 4
gpt4 key购买 nike

SQLSTATE[42S02]:未找到基表或 View :1146 表“patnership.zvss_new_transaction”不存在(SQL:select * from zvss_new_transaction where outletName = uchumi 和 transactionDate = 2017-02-13 限制 1)

当我尝试使用 laravel 实现多数据库连接时,我不断收到该错误,并且即使在环境和数据库文件中指定其他数据库后,它仍然会连接到默认数据库。我还在我的模型上添加了连接,但一直碰壁...我们将非常感谢您的帮助。

下面附上相关代码文件

数据库.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';
}

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

最佳答案

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

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.在connection下的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.在您的 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();

}

每当您想要连接到外部数据库时,都会使用它。详细化模型,并为其提供您要使用的连接,然后其余部分保持不变。

关于php - 使用php laravel框架连接多数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com