staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Does not work with multiple databse connections. #79

Closed buglinjo closed 4 years ago

buglinjo commented 4 years ago

The model I'm trying to get has a different connection than the model I'm referring from.
Please add functionality which will enable us to select connection per model in the array.

return $this
    ->setConnection('con1')
    ->hasManyDeep(
    Model1::class,
    [
        DB::connection('con1')->getDatabaseName() . '.pivot_table',
        <DB connection 'con1'> ... Model2::class,
    ]
);
staudenmeir commented 4 years ago

Please provide your actual relationship with the database connection of each model.

buglinjo commented 4 years ago

Figured it out with setting table name inside the model.

/**
* @var string
*/
protected $connection = 'con1';

/**
* Model constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
    parent::__construct($attributes);

    $this->table = DB::connection($this->getConnectionName())->getDatabaseName() . '.' . $this->getTable();
}

Thank you! Great package!