【问题标题】:Laravel Many-To-Many - Different databasesLaravel 多对多 - 不同的数据库
【发布时间】:2020-11-05 08:33:18
【问题描述】:

需要对一个问题有所了解...我正在尝试使用多对多关系从另一个数据库获取数据。

基本上,一个网站可以有很多模板,一个模板可以有很多网站。

网站模型:

class Site extends Model
{
    use HasFactory;

    /**
     * Database Connection Name
     */
    protected $connection = 'hub';

    /**
     * Model Table Name
     */
    protected $table = 'tbl_sites';

    /**
     * Model Primary Key
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'code', 'name', 'abbreviation', 'address', 'zipcode', 'town', 'geolocation_id', 'gps'
    ];

    /**
     * Returns associated SGC templates
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function sgc_templates()
    {
        return $this->belongsToMany('App\Models\SGC\Contracts\Templates\Template', 'sgc_contracts_templates_hasmany_sites', 'site_id', 'template_id');
    }
}

模板模型:

class Template extends Model
{
    use HasFactory;

    /**
     * Database Connection Name
     */
    protected $connection = 'sgc';

    /**
     * Model Table Name
     */
    protected $table = 'sgc_contracts_templates';

    /**
     * Model Primary Key
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'description', 'file_name'
    ];

    /**
     * Returns associated sites
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function sites()
    {
        return $this->belongsToMany('App\Models\Hub\Sites\Site', 'sgc_contracts_templates_hasmany_sites', 'template_id', 'site_id');
    }
}

如果我尝试获取与以下站点相关联的模板:Site::with('sgc_templates')->find(1),一切正常。

如果我尝试获取与模板相关联的网站:Template::with('sites')->find(1),则会出错。基本上说站点数据库中不存在数据透视表。模板和数据透视表位于sgc 连接/数据库中。

错误是:

Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hub.sgc_contracts_templates_hasmany_sites' doesn't exist (SQL: select `tbl_sites`.*, `sgc_contracts_templates_hasmany_sites`.`template_id` as `pivot_template_id`, `sgc_contracts_templates_hasmany_sites`.`site_id` as `pivot_site_id` from `tbl_sites` inner join `sgc_contracts_templates_hasmany_sites` on `tbl_sites`.`id` = `sgc_contracts_templates_hasmany_sites`.`site_id` where `sgc_contracts_templates_hasmany_sites`.`template_id` in (1))

很明显Template::with('sites')->find(1) 会进入错误的数据库,因为在错误中,“hub.sgc_contracts_templates_hasmany_sites”应该是“sgc.sgc_contracts_templates_hasmany_sites”。

有人可以帮我解决这个问题吗? :|

谢谢

【问题讨论】:

    标签: php laravel many-to-many


    【解决方案1】:

    找到了解决方法。似乎多对多只适用于 1 个方向(?)。

    Github Issue

    Workaround

    感谢大家的帮助。

    【讨论】:

      【解决方案2】:

      你需要告诉 Eloquent 你想使用其他数据库,试试这样的

      return $this->belongsToMany('App\Models\Hub\Sites\Site', 'sgc.sgc_contracts_templates_hasmany_sites', 'template_id', 'site_id');
      

      然后检查它是否尝试查询sgc db。

      如果仍然没有帮助,试试这个https://stackoverflow.com/a/60060726/7892040

      【讨论】:

      • 嗨,感谢您的贡献...我试过了,但没有用。
      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      相关资源
      最近更新 更多