假設模型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是關聯字段
}
}