【发布时间】:2014-03-12 09:02:34
【问题描述】:
我正在开发社交应用前端 Angualar、后端 laravel 和数据库 Mongodb。我有这样的模型:
Hoots
-----------------
- _id
- content
- publish_id
Article
-----------------
- _id
- content
- publish_id
Story
-----------------
- _id
- content
- publish_id
Publish
-----------------
- _id
- post_id
- type
- user_id
publish 中的 Post id 属于 hoots 、 article 和 story 中的 _id ,其中 type 表示它是 hoot 、 article 还是 story。
我有这样的模型
//Article model
class Article extends Eloquent {
public function getpublish(){
return $this->hasMany('Publish','post_id');
}
}
//Story model
class Story extends Eloquent {
public function get_publish(){
return $this->hasMany('Publish','post_id');
}
}
//Hoots model
class Hoots extends Eloquent {
public function get_publ(){
return $this->hasMany('Publish','post_id');
}
}
//Publish model
class Publish extends Eloquent {
public function getdata(){
return $this->BelongsTo('Hoots','Article','Story','publish_id');
}
}
我正在使用
Publish::with('getdata')->where('user_id',Auth::user()->id)->get();
使用这个我只能在一个模型中获取发布数据以及 post_id 对应的数据,即仅 hoots。我想从所有三个表中得到这个。
我想获取发布模型数据及其对应的 post_id 数据。如何使用 eloquent 完成这个单一查询。
【问题讨论】:
-
很难弄清楚你的要求。你能提供一张你期望的结果表吗?\
-
@Gaz_Edge 我已经更新了代码。请查看。如何从具有外键 publish_id 的三个不同模型中获取发布模型数据以及 post_id 对应数据?
标签: mongodb laravel-4 eloquent