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

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

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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是关联字段
}
}

 

posted @ 2020-09-07 10:32  小林不会飞  阅读(1136)  评论(0编辑  收藏  举报