例如 有一个订单表order ,和订单下的相关商品表 order_goods
既可以搜索订单,又可以搜索订单下的商品
if (isset($data['keywords']) && $data['keywords'] != '') {
$sql->where('order_sn', '=', $data['keywords']);
// $sql->has('toGoods.goods_name','like','"%'.$data['keywords'].'%"');
$sql->orWhereHas ('toGoods',function ($q) use($data){
$q->where('goods_name', 'like', '%' . trim($data['keywords']) . '%');
});
}
注:如果要用and 可以使用 whereHas即可
生成sql语句
select count(*) as aggregate
from
`dl_order_info`
where
`id` > 0
and
`order_sn` = '南极人短袖t恤男'
or exists
(select * from `dl_order_goods` where `dl_order_info`.`id` = `dl_order_goods`.`order_id` and `goods_name` like '%南极人短袖t恤男%')
and
`is_refund` = '0'
and
`user_id` = 11