Home  >  Article  >  Backend Development  >  Association between models in different databases in Laravel, laravel model association_PHP tutorial

Association between models in different databases in Laravel, laravel model association_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:08891browse

Association between models in different Laravel databases, laravel model association

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099826.htmlTechArticleLaravel is associated with models in different databases. Laravel model association assumes that the relationship between ModelA and ModelB is BelongsTo. If the same Belongs to a database connection then public function a(){...
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