Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-12 08:59:08
Original
946 people have browsed it

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>
Copy after login

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>
Copy after login

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(){...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template