Laravel 8 Multiple Database Connection Example
Hi there,
Immediately, i we’ll present you laravel eight a number of database connection instance. This text gives you easy instance of laravel eight a number of database connection instance.
you’ll laravel eight a number of database connection instance. On this article, we’ll implement a laravel eight a number of database connection instance.
So let’s observe few step to create instance of laravel eight a number of database connection instance.
Set ENV File
it’s essential set configuration variable on .env file.
.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
Use ENV Variable
config/database.php
<?php use IlluminateSupportStr; 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, ],
Use Database A number of Connection:
Database Reference to migration
Default:
<?php public operate up() { Schema::create('weblog', operate (Blueprint $desk) { $table->increments('id'); $table->string('title'); $table->string('physique')->nullable(); $table->timestamps(); }); }
Second Database Connection:
<?php public operate up() { Schema::connection('mysql2')->create('weblog', operate (Blueprint $desk) { $table->increments('id'); $table->string('title'); $table->string('physique')->nullable(); $table->timestamps(); }); }
Database Reference to mannequin
Default:
<?php namespace App; use IlluminateDatabaseEloquentModel; class Weblog extends Mannequin { }
Second:
<?php namespace App; use IlluminateDatabaseEloquentModel; class Weblog extends Mannequin { protected $connection = 'mysql2'; }
Database Connection in Controller
Default:
<?php class BlogController extends BaseController { public operate getRecord() { $blogModel = new Weblog; $discover = $blogModel->discover(1); return $discover; } }
Second:
<?php class BlogController extends BaseController { public operate getRecord() { $blogModel = new Weblog; $blogModel->setConnection('mysql2'); $discover = $blogModel->discover(1); return $discover; } }
Database Reference to Question Builder
Default:
$blogs = DB::desk("weblog")->get(); print_r($blogs);
Second:
$blogs = DB::connection('mysql2')->desk("weblog")->get(); print_r($blogs);
I hope it is going to help you…
Komentar
Posting Komentar