我正在开发社交应用程序前端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
帖子中的发布ID属于hoots,文章和Story中的_id,其中类型表示是hoot,文章或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对应的数据,即仅是挂钩。我希望所有三个表都这样。
我想获取发布模型数据及其相应的post_id数据,如何使用雄辩的方法完成此单个查询。
我认为您可能没有正确建立人际关系;
//Publish model
class Publish extends Eloquent {
public function hoot(){
return $this->HasMany('Hoots','publish_id');
}
}
public function article(){
return $this->HasMany('article','publish_id');
}
}
public function story(){
return $this->HasMany('stort','publish_id');
}
}
Publish::with(array('hoot', 'story', 'article'))->where('user_id',Auth::user()->id)->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句