如何在laravel中加入查询

时间:2015-05-28 09:27:52

标签: php mysql laravel-4 eloquent

商业模式

   public function groupTag()
   {
        return $this->belongsTo('GroupTag');
   }

组标记模型

    public function tag()
    {
        return $this-    >belongsToMany('Tag','group_tag_tags','group_tag_id','tag_id')
        ->withTimestamps();
    }

    public function business()
    {
        return $this->hasOne('Business');
    }

标记模型

   public function groupTag()
    {
        return $this->belongsToMany('Group','group_tag_tags','group_tag_id','tag_id')->withTimestamps();
    }

现在我如何将此查询运行到laravel项目

SELECT * FROM business`为b, group_tags为gt, group_tag_tags为gtt, 标签为t 其中b.group_tag_id = gt.id和gt.id = gtt.group_tag_id和gtt.tag_id = t.id和t.id = 36或b.name如'%a%'和b.city_id = 5 group by b。 ID'

1 个答案:

答案 0 :(得分:1)

DB::table('businesses')
        ->join('group_tags','group_tags.id','=','businesses.group_tag_id')
        ->join('group_tag_tags','group_tag_tags.group_tag_id','=','group_tags.id')
        ->join('tags','tags.id','=','group_tag_tags.tag_id')
        ->where('tags.id',"=",36)
        ->where('b.name',"LIKE",'%a%')
        ->where('b.city_id',"=",5)
        ->select('businesses.id','businesses.name','businesses.description','businesses.image')
        ->groupBy('businesses.id')