您的位置:首页 > 编程语言 > PHP开发

Laravel5.5 根据关联模型的字段模糊查询

2019-06-21 15:26 549 查看

 

比如后台文章列表要根据文章内容模糊筛选:

[code]$where1 = [];
$where2 = [];

// 查询条件:帖子内容
if ( !empty($content) ) {
$where1 = $content;
}

$perPage = $request->input('per_num', 10);  // 每页页码
$data    = TopicReply::where($where2)
->whereHas('TRtoData', function($query) use ($where1){
if ( !empty($where1) ) {
$query->where('content', 'like', '%'.$where1.'%');
}
})
->with('TRtoData')
->orderBy('id', 'desc')
->paginate($perPage);

 

如果要带关联表里的字段成为查询条件,用whereHas,如果要带关联表的信息,再with一下就可以了!

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: