Multiple Database Connections using Laravel

Multiple Database Connections using Laravel

In this Post We Will Explain About is Multiple Database Connections using Laravel With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to connect multiple database in laravel Example

In this post we will show you Best way to implement How to connect multiple database in laravel 5.5, hear for laravel use multiple database with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Step 1 : Install Laravel

In some multiple protect we need to use database connect to multiple databases for same web-application. Lets Display how to use step by step multiple database using laravel 5.5 First of allInstall We laravel Web-application as well as simple connect with primary main database

Also Read :   Laravel get single value from database

Step 2 : MAke a database in Mysql

Make simple second database in We use own phpmyadmin

Step 3 : config/database.php

Generally simple provide We database connection Information in We config/database.php

Like as a …

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'), //host
            'port' => env('DB_PORT', '3306'),//port
            'database' => env('DB_DATABASE', 'forge'),//dbname
            'username' => env('DB_USERNAME', 'forge'),//username
            'password' => env('DB_PASSWORD', ''),//password
            'unix_socket' => env('DB_SOCKET', ''),//socket
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'mysql2' => [
            'driver' => 'mysql',
            'host' => 'localhost', //hostname
            'port' => '3306',		//port
            'database' => 'multiple',//databasename
            'username' => 'root', //username
            'password' => 'root', //password
            'unix_socket' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

Step 4 :

Use We database connection in Schema to Make table for We new tables for new database

Also Read :   Cross Header Access Control Allow Origin missing


//laravel simple create migrate up methods
public function up()
    {
        Schema::connection('mysql2')->create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('role')->nullable();
            $table->string('capabilities')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse simple laravel the migrations.
     *data type
     * @return void
     */
	 //laravel simple create migrate Down methods
    public function down()
    {
        Schema::dropIfExists('roles');
    }

Step 5 : run Project

Run We need to browser laravel migration to Make table

php artisan migrate --database="YourDatabaseConnName"

Step 6 : database Connection

All the Product We model with connection

<?php
//import namespace
namespace App;
//model use
use Illuminate\Database\Eloquent\Model;

//simple extends class
class Role extends Model
{
   protected $connection = 'mysql2';
    /**
     * The all laravel attributes that are some mass assignable.
     *
     * @var simple php array
     */
    protected $fillable = [
        'role',
        'capabilities'
    ];
}

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   Laravel Autocomplete Search Twitter Typeahead Example





Example

I hope you have Got What is How to connect multiple database in laravel 5.5 And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.