Skip to content

Commit fafd0ab

Browse files
committed
join operation
1 parent 17e1579 commit fafd0ab

File tree

6 files changed

+32
-2
lines changed

6 files changed

+32
-2
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ DB_DATABASE=homestead
1212
DB_USERNAME=homestead
1313
DB_PASSWORD=secret
1414

15+
DB_DATABASE2=yourseconddatabase
16+
17+
DB_HOST_SQLSRV=127.0.0.1
18+
DB_PORT_SQLSRV=1433
19+
DB_DATABASE_SQLSRV=yoursqlsrvdatabase
20+
DB_USERNAME_SQLSRV=
21+
DB_PASSWORD_SQLSRV=
22+
1523
BROADCAST_DRIVER=log
1624
CACHE_DRIVER=file
1725
SESSION_DRIVER=file

app/Http/Controllers/PublishingController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ public function type()
2020
$data['type'] = Type::with(['posts', 'mails'])->get();
2121
return response()->json($data);
2222
}
23+
24+
public function joinsameserver()
25+
{
26+
$data['post'] = Post::join('laramultidb2.types as db2type', 'posts.type_code', '=', 'db2type.code')->get();
27+
return response()->json($data);
28+
}
2329
}

app/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Type extends Model
88
{
9-
protected $connection = 'mysql';
9+
protected $connection = 'mysql2';
1010

1111
public function posts()
1212
{

config/database.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@
5454
'engine' => null,
5555
],
5656

57+
'mysql2' => [
58+
'driver' => 'mysql',
59+
'host' => env('DB_HOST', '127.0.0.1'),
60+
'port' => env('DB_PORT', '3306'),
61+
'database' => env('DB_DATABASE2', 'forge'),
62+
'username' => env('DB_USERNAME', 'forge'),
63+
'password' => env('DB_PASSWORD', ''),
64+
'unix_socket' => env('DB_SOCKET', ''),
65+
'charset' => 'utf8mb4',
66+
'collation' => 'utf8mb4_unicode_ci',
67+
'prefix' => '',
68+
'strict' => true,
69+
'engine' => null,
70+
],
71+
5772
'pgsql' => [
5873
'driver' => 'pgsql',
5974
'host' => env('DB_HOST', '127.0.0.1'),

database/migrations/2018_05_04_092640_create_table_type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateTableType extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('types', function (Blueprint $table) {
16+
Schema::connection('mysql2')->create('types', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('code')->index();
1919
$table->string('name');

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
Route::get('publishing', 'PublishingController@index');
44
Route::get('type', 'PublishingController@type');
5+
Route::get('joinsameserver', 'PublishingController@joinsameserver');

0 commit comments

Comments
 (0)