Это должно сработать, если вы
- Укажите, какое соединение использует каждая модель, используя свойство
$connection
. - Укажите пользовательскую сводную модель в отношениях
belongsToMany
.
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
protected $connection = 'auth'; // database name
protected $table = 'documents'; // table name
public function items()
{
return $this->belongsToMany(Item::class, 'document_item')->using(DocumentItem::class);
}
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $connection = 'inventory'; // database name
protected $table = 'items'; // table name
public function documents()
{
return $this->belongsToMany(Document::class, 'document_item')->using(DocumentItem::class);
}
}
use Illuminate\Database\Eloquent\Relations\Pivot;
class DocumentItem extends Pivot
{
protected $connection = 'inventory'; // database name
protected $table = 'document_item'; // table name
public function document()
{
return $this->belongsTo(Document::class);
}
public function item()
{
return $this->belongsTo(Item::class);
}
}