根据关联表字段查询
$lists = Orders::with(['goods:order_id,name']) ->whereHasIn('goods', function ($query) use ($goodsName) { if ($goodsName) { $query->where('name', 'like', '%' . $goodsName . '%'); } }) ->select(['id', 'order_no']) ->paginate(10) ->toArray();
用到的扩展 dcat/laravel-wherehasin