Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel有许多跨多个连接的连接_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php Laravel有许多跨多个连接的连接

Php Laravel有许多跨多个连接的连接,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我想在第二个表Offer\u related上检索与Offer相关的s,因为我无法更改Offer表的架构 我在不同的连接上有两个数据库,offers在一个上,而offer\u related在另一个上 为了便于讨论,为了在我的示例中清楚地说明哪些数据库可以更改,哪些数据库不能更改,我将对数据库进行如下命名 包含的数据库提供以后称为不可变的 包含提供相关的数据库,此后称为可变 示例模式如下所示 connection1.mutable.offer_related offer_id | rela

我想在第二个表
Offer\u related
上检索与
Offer
相关的
s,因为我无法更改
Offer
表的架构

我在不同的连接上有两个数据库,
offers
在一个上,而
offer\u related
在另一个上

为了便于讨论,为了在我的示例中清楚地说明哪些数据库可以更改,哪些数据库不能更改,我将对数据库进行如下命名

  • 包含
    的数据库提供
    以后称为
    不可变的
  • 包含
    提供相关
    的数据库,此后称为
    可变
示例模式如下所示

connection1.mutable.offer_related

offer_id | related_offer_id
---------------------------
1        | 2
1        | 3

connection2.immutable.offers

id | name
---------------------------
1  | foo
2  | bar
3  | baz
我假设这是一段属于自己的关系,但我似乎无法正确理解

return $this->belongsToMany(Offer::class, 'immutable.offer', 'id');

// Syntax error or access violation: 1066 Not unique table/alias: 'offer'
我还尝试用自定义查询手动构建一个belongstomy关系,但没有成功

我想打个电话

Offer::find(1)->related; // Offer(2), Offer(3)

将关系更改为:

    return $this->belongsToMany(Offer::class, 'mutable.offer_related', 
                                'offer_id', 'related_offer_id');

原始查询试图在不使用关系表的情况下建立关系(与offer\u相关)。这就是问题所在。

我也有这个问题,然后用一个特点解决了:

<?php 

namespace App\Traits\Model;

use Illuminate\Support\Str;

trait HasCrossDatabaseRelation {

    public function getTable() {
        if (! isset($this->table)) {
            $this->table = str_replace(
                '\\', '', Str::snake(Str::plural(class_basename($this)))
            );
        }

        $configConnections = config('database.connections');

        $databaseName = $configConnections[$this->getConnectionName()]['database'];

        return $databaseName . "." . $this->table;
    }

}

另一个解决方案是创建特征:

trait HasCrossDbRelationships {

  /**
   * Define a one-to-many relationship with connection attribute
   *
   * @param  string  $connection
   * @param  string  $related
   * @param  string  $foreignKey
   * @param  string  $localKey
   * @return \Illuminate\Database\Eloquent\Relations\HasMany
   */
  public function hasManyCrossDb($connection, $related, $foreignKey = null, $localKey = null)
  {
      $instance = $this->newRelatedInstance($related);
      $instance->setConnection($connection);

      $foreignKey = $foreignKey ?: $this->getForeignKey();

      $localKey = $localKey ?: $this->getKeyName();

      return $this->newHasMany(
          $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
      );
  }

}

你能试试这个,看看是否有效吗<代码>返回$this->belongTomany(Offer::class,'mutable.Offer_-related','Offer_-id','related_-Offer_-id')这很有效,太好了!贴出答案,我会接受。非常感谢。好的!我已经做到了。非常感谢。