发布于 5年前

Laravel 关联关系 whereHas 跨数据库使用

Databases: db1(default)、db2 Tables: db1.a、db2.b

A.php

<?php

namespcae App\A;

class A extends model
{
    public function b()
    {
        return $this->morphToMany(B::class);
    }
}

B.php

<?php

namespcae App\B;

class B extends model
{
    protected $connect = 'db2';
}

How to use whereHas

A::whereHas('b', function ($query) { 
    $query->from('db2.b')->where(....); 
});
©2020 edoou.com   京ICP备16001874号-3