Maison >développement back-end >tutoriel php >Laravel不同数据库的模型之间关联,laravel模型关联_PHP教程
假设ModelA和ModelB之间是BelongsTo的关系
如果同属于一个数据库连接
那么
<span>public</span> <span>function</span><span> a(){ </span><span>return</span> <span>$this</span>->belongsTo("ModelA"<span>) }</span>
如果两个模型属于不同的数据库
那么
<span>public</span> <span>function</span><span> a() { </span><span>$instance</span> = <span>new</span><span> ModelA; </span><span>$instance</span>->setConnection(<span>$a_conn</span><span>); </span><span>$query</span> = <span>$instance</span>-><span>newQuery(); </span><span>return</span> <span>new</span> BelongsTo(<span>$query</span>, <span>$this</span><span>); }</span>