这是我在控制器中的功能
public function ajaxResponse (Request $request) {
if ($request->ajax()) {
$mat_id = $request->input('mat');
$cat_id = $request->input('cat');
$met_id = $request->input('met');
if ($cat_id != null) {
$products = Product::whereHas("categories", function ($query) use ($cat_id) {
$query->whereIn('category_id', explode(',', $cat_id));
})->get();
}
if ($mat_id != null) {
$products = Product::whereHas("productMaterial", function ($query) use ($mat_id) {
$query->whereIn('product_material_id', explode(',', $mat_id));
})->get();
}
if ($met_id != null) {
$products = Product::whereHas("productionMethod", function ($query) use ($met_id) {
$query->whereIn('production_method_id', explode(',', $met_id));
})->get();
}
if ($cat_id == null && $mat_id == null && $met_id == null) {
$products = Product::all();
}
$prod = view('partials.ajaxProducts', ['products' => $products])->render();
}
return response()->json(['prod' => $prod]);
}
所有记录都根据 $cat_id
, $mat_id
, $met_id
这里categories和productmaterial与product有多对多关系,productionmethod与product有一对多关系我想通过ajax请求结合这三种关系过滤数据
2条答案
按热度按时间eni9jsuy1#
lztngnrs2#
您可以使用: