Association between models in different databases in Laravel

WBOY
Release: 2016-07-29 09:03:29
Original
1337 people have browsed it

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

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.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!