Multiple Database Connections using Laravel

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

Multiple Database Connections using Laravel

In this Post We Will Explain About is Multiple Database Connections using Laravel With Example and Demo.Welcome on Pakainfo.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!.

Read Also:  Update multiple records using Laravel

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

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

Read Also:  Fadein Fadeout example using Jquery Example

//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

Read Also:  AngularJs Clone Array

<?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

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(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *