Laravel Eloquent: how to pass the name of a relationship model connection

I have two models, one mongo model extending the Jenssegers \ Model and another sql model extending the Illuminate \ Model. This sql model does not have a connection name defined as ours with several database connections that have the same table in each database.

Mongo Model Comment.php

<?php namespace App\Models\Mongo; use Jenssegers\Mongodb\Eloquent\Model as Eloquent; class Comment extends Eloquent { /** @var string Mongo Connection Name */ protected $connection = 'mongodb'; /** @var string Mongo Collection Name */ protected $collection = 'comments'; /** @var bool Enable/Disable Timestamp */ public $timestamps = true; /** @var date Date format */ protected $dateFormat = 'Ymd H:i:s'; public function userProfile() { return $this->belongsTo('\\App\\Models\\Sql\\UserDefaultProfile', 'created_by', 'user_code'); } } 

Sql Model UserProfile.php

 <?php namespace App\Models\Sql; use Illuminate\Database\Eloquent\Model; use Jenssegers\Mongodb\Eloquent\HybridRelations; class UserDefaultProfile extends Model { use HybridRelations; /** @var string Table Name */ protected $table = 'user_default_profile'; /** @var bool Enable/Disable Timestamp */ public $timestamps = false; } 

I have several database connections, add to Capsule

 try { $getDatabaseList = StoreDatabaseCredential::all(); } catch (Exception $exception) { } foreach ($getDatabaseList as $database) { if (strtolower($database->database_type) == 'mysql') { $db->addConnection([ 'driver' => 'mysql', 'host' => $database->database_router_read_host, 'port' => $database->database_router_read_port, 'database' => $database->database_name, 'username' => $database->database_user, 'password' => $database->database_password, 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', ], $database->connection_name); } } 

therefore several database connections are available

Now the problem is that I caused eloquence with attitude, I get Database [Default] not configured . I get this error because the UserProfile Model has no specific connection. So, please, someone who can tell how to pass the name of the connection to the relationship model.

 try { $comments = Comment::where([ 'in_reply_to_content_id' => $contentId, 'super_store_id' => $superStoreId, 'is_deleted' => 0 ])->with([ 'userProfile' => function ($query) use ($dbConnectionName) { $query->select('id', 'user_code', 'mobile', 'name', 'profile_pic_url'); } ])->skip($offset)->take($itemsPerPage)->orderBy('created_at', 'desc')->get(); Utils::printData($contentComments); exit(); } catch (\Throwable $exception) { Utils::printData($exception->getMessage()); exit(); } 

So is it possible to do something similar with an attitude

 with([ 'userProfile' => function ($query) use ($dbConnectionName) { $query->setConnection($dbConnectionName)->select( 'id', 'user_code', 'mobile', 'name', 'profile_pic_url' ); } ])->skip($offset)->take($itemsPerPage)->orderBy('created_at', 'desc')->get(); 
+4
source share
2 answers

You can extend the base class Model for your UserProfile model, override getConnectionName and get the connection name from the static class variable specified using the static method

eg:

 use Illuminate\Database\Eloquent\Model; class RelationWithChangingConnection extends Model { static $connectionName; static public function setConnectionNameForRelation($connectionName) { static::$connectionName = $connectionName; } public function getConnectionName() { $this->connection = static::$connectionName; return parent::getConnectionName(); } } class UserDefaultProfile extends RelationWithChangingConnection { //... } 

and then

 with([ 'userProfile' => function ($query) use ($dbConnectionName) { UserDefaultProfile::setConnectionNameForRelation($dbConnectionName); $query->setConnection($dbConnectionName)->select( 'id', 'user_code', 'mobile', 'name', 'profile_pic_url' ); } ])... 

However, it looks ugly. You can also switch database connections through the switching configuration files, as suggested here

UPD: Here I came across another possible solution, but have not tested it yet - you can write your relationship subquery as follows:

 ... ->with([ 'userProfile' => function ($query) use ($dbConnectionName) { $query->from('[yourdatabase].[dbtable]')->select('id','user_code', 'mobile', 'name', 'profile_pic_url'); } ... 

and it will connect to the database and db table that you installed.

+2
source

Yes, it is possible to set the connection name if your model extends to Illuminate \ Database \ Eloquent \ Model;

 $profile = UserDefaultProfile::setConnection('your_connection_name_goes_here')->where('','')->select(''); 
-1
source

Source: https://habr.com/ru/post/1275841/


All Articles