laravel_orm

表结构

article(id,title,creatTime)

t_type(id,name)

industry(id,name)

中间表

t_type_articles(id,t_type_id,artcle_id)

article_industry__rel(id,industry_id,article_id)

在Article类中建立关系

public function rel_type(){
return $this->belongsToMany('App\Models\Type','t_type_articles',"article_id","t_type_id");
}
public function rel_industry(){
return $this->belongsToMany('App\Models\Industry','article_industry_rel','article_id','industry_id');
}
在Industry类中建立关系



查询文章结果集中包含关系的结果(包含文章具体属于那些类型)
//        $result=$this
// //->searchWhere($parm)
// ->whereIn("id",array(1,17680,17679,17677,17676,17675,17674,17673,17672,17671,17670))
// ->with(array('rel_type'=>function($query){
// $query->where('t_type.name','=','报告');
// }))
// //
// //->has('rel_type',function($qs){})
// //->whereIn("id",array(17680,17679,17677,17676,17675,17674,17673,17672,17671,17670))
// // ->where('t_type.name','=','报告')
// //->searchWhere($parm)
// ->get();
查询指定type和industry的文章
$instance=$this->searchWhere($parm)

->whereHas('rel_type',function($q){
$q->where('name','=','政府文件');
})
->whereHas('rel_industry',function($q){
$q->where('name','=','房地产');
})
->get();


posted @ 2017-03-20 12:10  米斯特尔曾  阅读(88)  评论(0)    收藏  举报