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是關聯字段
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM