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

 

相关文章:

  • 2021-12-22
  • 2021-12-27
  • 2021-08-24
  • 2021-11-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
相关资源
相似解决方案