Home  >  Article  >  Backend Development  >  Association between models in different databases in Laravel

Association between models in different databases in Laravel

WBOY
WBOYOriginal
2016-07-29 09:03:291365browse

Suppose the relationship between ModelA and ModelB is BelongsTo

If they both belong to the same database connection

then

<span>public</span><span>function</span><span> a(){
    </span><span>return</span><span>$this</span>->belongsTo("ModelA"<span>)          
}</span>

If the two models belong to different databases

then

<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>

The above introduces the relationship between the models of different databases in Laravel, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn