6

Datebase 1 = db1 Database 2 = db2

I have two databases connection, I wanted to run a eloquent or DB which joining connection1 and connection 2 table(db1.users and db2.users), and check connection1 id = connection 2 or not.

can someone guide me how to do that?

Appreciate that if someone could guide me what to do.

db1 user id name ori_id

db2 ori_user id name

select db2.ori_user.name from db1.user join db1.user on db1.user.ori_id = db2.ori_user.id

1
  • yes, i can see. Is that any where can join two table from different database? Commented Nov 28, 2016 at 4:13

2 Answers 2

4

Yes, it's possible as long as they are on the same server, for example:

$result = \DB::table('db1.users')
    ->join('db2.users', 'db2.users.id', '=', 'db1.users.id')
    ->select('db1.users.id as db1_id', 'db1.users.email as db1_email', 'db2.users.*')
    ->get();

Make sure the user has access/privilege to use both databases.

Sign up to request clarification or add additional context in comments.

2 Comments

OP is asking for two separate databases.
Yes, I'm concerned about that and answered that exactly. Have you even checked my answer or you didn't understand?
0

in database.php

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'customers'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'customers2'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

here customers is database1 and customers2 is database2

in controller

 $data=DB::select('select customers2.ori_user.name from customers.user join customers2.ori_user on customers.user.ori_id = customers2.ori_user.id');

or

$data=DB::select('your_query');

Youtube link:https://www.youtube.com/watch?v=Kgl3FzqP1Ps&feature=youtu.be

8 Comments

this is just join two same database tables, is that possible to make it join from different database table.
oh sorry my mistake i thought 2 table in same database..ok i will try if i can i will provide u
thanks for your fast response, i have tried this, is not working. do you have others way?
what is the error you r getting?? can kindly share??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.