Many to many between 2 different databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DB_CONNECTION=pgsql | |
DB_HOST=127.0.0.1 | |
DB_PORT=5433 | |
DB_DATABASE=diesel_bot | |
DB_USERNAME=postgres | |
DB_PASSWORD=postgres | |
DB_CONNECTION_SECOND=mysql | |
DB_HOST_SECOND=127.0.0.1 | |
DB_PORT_SECOND=3306 | |
DB_DATABASE_SECOND=diesel_forum | |
DB_USERNAME_SECOND=root | |
DB_PASSWORD_SECOND=password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'pgsql' => [ | |
'driver' => 'pgsql', | |
'url' => env('DATABASE_URL'), | |
'host' => env('DB_HOST', '127.0.0.1'), | |
'port' => env('DB_PORT', '5432'), | |
'database' => env('DB_DATABASE', 'forge'), | |
'username' => env('DB_USERNAME', 'forge'), | |
'password' => env('DB_PASSWORD', ''), | |
'charset' => 'utf8', | |
'prefix' => '', | |
'prefix_indexes' => true, | |
'search_path' => 'public', | |
'sslmode' => 'prefer', | |
], | |
'forum_db' => [ | |
'driver' => 'mysql', | |
'url' => env('DATABASE_URL'), | |
'host' => env('DB_HOST_SECOND', '127.0.0.1'), | |
'port' => env('DB_PORT_SECOND', '3306'), | |
'database' => env('DB_DATABASE_SECOND', 'forge'), | |
'username' => env('DB_USERNAME_SECOND', 'forge'), | |
'password' => env('DB_PASSWORD_SECOND', ''), | |
'unix_socket' => env('DB_SOCKET_SECOND', ''), | |
'charset' => 'utf8mb4', | |
'collation' => 'utf8mb4_unicode_ci', | |
'prefix' => '', | |
'prefix_indexes' => true, | |
'strict' => true, | |
'engine' => null, | |
'options' => extension_loaded('pdo_mysql') ? array_filter([ | |
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), | |
]) : [], | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
/** | |
* Post model for the second database connection | |
*/ | |
class Forum extends Model | |
{ | |
use HasFactory; | |
protected $connection = 'forum_db'; | |
protected $table = 'ibf_forums'; | |
/** | |
* Subscribers belongs to the forum. | |
*/ | |
public function subscribers() | |
{ | |
return $this->belongsToMany(Subscriber::class, 'pgsql.forum_subscriber', 'forum_id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class Subscriber extends Model | |
{ | |
use HasFactory; | |
protected $connection = 'pgsql'; | |
protected $table = 'subscribers'; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* u/var array<int, string> | |
*/ | |
protected $fillable = [ | |
'name', | |
'chat_id', | |
]; | |
/** | |
* Forums subscribed. | |
*/ | |
public function forums() | |
{ | |
return $this->belongsToMany(Forum::class, 'pgsql.forum_subscriber', 'subscriber_id'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment