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 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!.

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

Read Also:  Display Live Date Time using JQuery and JavaScript

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:  CodeIgniter 3 PDF Generate Tutorial With 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:  sort query order by in codeigniter Examples

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


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Toggle Switch Inside Bootstrap Ajax Example
  2. Read Also:  How to use multiple databases in Laravel
  3. Read Also:  Simple Angular 6 Routing and Single Page View Tutorial
  4. Read Also:  jquery validate select dropdown Example
  5. Read Also:  php slideshow in javascript source code
  6. Read Also:  WordPress AJAX - Wordpress jQuery AJAX POST Request
  7. Read Also:  jQuery Ajax GET & POST REQUEST Methods PHP MySQLi
  8. Read Also:  Laravel Multiple Database Connections Step By Step
  9. Read Also:  Installing APC for php Accelerator xampp/WAMP
  10. Read Also:  Laravel Multiple Database Connections Step By Step