Home > php教程 > php手册 > Laravel不同数据库的模型之间关联,laravel模型关联

Laravel不同数据库的模型之间关联,laravel模型关联

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 08:46:13
Original
1165 people have browsed it

Laravel不同数据库的模型之间关联,laravel模型关联

假设ModelA和ModelB之间是BelongsTo的关系

 

如果同属于一个数据库连接

那么

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

如果两个模型属于不同的数据库

那么

<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

 

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
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template