laravel如何实现不同数据库的模型进行关联

假设模型Replenishment和模型Product要关联,并且模型Replenishment和模型Product的表是在不同数据库

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Replenishment extends Model
{
    protected $fillable = ['g_code'];
    public $table = 'replenishment';
    protected $connection = "mm"; //config/database.php中的connections数组中的
    public function product(){
        $connection = 'mysql';//config/database.php中的connections数组中的
        return $this->setConnection($connection)->belongsTo(Product::class,'g_code','code');//Product::class就是要关联的模型,g_code和code是关联字段
}
}
踩过这个坑,还有下一个坑等着你,这一路就是给自己填坑,坑填多了,也就习惯了,直到这一路平坦了,也就无怨无悔了。
原文地址:https://www.cnblogs.com/xiaofeilin/p/13625462.html