假设模型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是关联字段
}
}