Laravel根据关联表条件进行查询

$keyword = $this->filters['keyword'];
$options = $this->parseOptions();
$datas = TableModel::with('hasBook')
   ->with('hasGradeCell.hasGrade')
   ->when($keyword,function($query)use($keyword){
       return $query->where('title','like','%'.$keyword.'%');
   })
   ->when($options,function ($query) use ($options){
       if (isset($options['cell_id'])) {
           return $query->where('cell_id', $options['cell_id']);
       }
       if (isset($options['grade_id'])) {
           return $query->whereHas('hasGradeCell',function ($query) use ($options){
               return $query->whereHas('hasGrade',function ($query) use ($options){
                   return $query->where('id',$options['grade_id']);
               });
           });
       }

   });
$counts            = $datas->count();

$datas = $datas->where('type', 1)
       ->orderBy($this->filters['sort'], $this->filters['sort_by'])
       ->paginate($this->filters['limit'])
       ->appends($this->filters);


博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论