当先锋百科网

首页 1 2 3 4 5 6 7

在本章中将执行Laravel 8连接多数据库操作。我们将在.env文件上添加数据库连接配置。

设置 env变量:

在这里,需要在.env文件上设置配置变量。

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=root
   
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=mydatabase2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=root

使用env变量:

现在,当我们在env文件中创建变量时,我们需要在配置文件上使用该变量,因此让我们打开database.php文件并添加新的连接信息:

config/database.php

<?php
  
use Illuminate\Support\Str;
  
return [
   
    'default' => env('DB_CONNECTION', 'mysql'),
   
    'connections' => [
        .....
   
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
        'mysql2' => [
            'driver' => env('DB_CONNECTION_SECOND'),
            'host' => env('DB_HOST_SECOND', '127.0.0.1'),
            'port' => env('DB_PORT_SECOND', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],
.....        

使用数据库多个连接:

在这里,我将为您提供如何用作多个连接的简单示例:

数据库连接与迁移:

<?php
.....
public function up()
{
    Schema::create('blog', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('body')->nullable();
        $table->timestamps();
    });
}
.....

第二:

<?php
.....
public function up()
{
    Schema::connection('mysql2')->create('blog', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('body')->nullable();
        $table->timestamps();
    });
}
.....

数据库连接模型

<?php
   
namespace App;
  
use Illuminate\Database\Eloquent\Model;
   
class Blog extends Model
{
}

第二:

<?php
   
namespace App;
  
use Illuminate\Database\Eloquent\Model;
   
class Blog extends Model
{
    protected $connection = 'mysql2';
}

控制器中的数据库连接

默认:

<?php
  
class BlogController extends BaseController
{
    public function getRecord()
    {
        $blogModel = new Blog;
        $find = $blogModel->find(1);
        return $find;
    }
}

第二:

<?php
  
class BlogController extends BaseController
{
    public function getRecord()
    {
        $blogModel = new Blog;
        $blogModel->setConnection('mysql2');
        $find = $blogModel->find(1);
        return $find;
    }
}

数据库连接查询构建器

默认:

$blogs = DB::table("blog")->get();
print_r($blogs);

第二:

$blogs = DB::connection('mysql2')->table("blog")->get();
print_r($blogs);

https://www.learnfk.com/article-laravel-8-multiple-database-connection-exampleexample