在laravel中通过ajax请求过滤数据

vd2z7a6w  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(346)

这是我在控制器中的功能

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请求结合这三种关系过滤数据

eni9jsuy

eni9jsuy1#

public function ajaxResponse(Request $request){
        if ($request->ajax()) {
            $mat_id = $request->input('mat');
            $cat_id = $request->input('cat');
            $met_id = $request->input('met');

            $products = Product::query();
            if ($cat_id != null) {
                $products->whereHas("categories", function ($query) use ($cat_id) {
                    $query->whereIn('category_id', explode(',', $cat_id));
                })->get();
            }
            if ($mat_id != null) {
                 $products->whereHas("productMaterial", function ($query) use ($mat_id) {
                    $query->whereIn('product_material_id', explode(',', $mat_id));
                })->get();
            }
            if ($met_id != null) {
                $products->whereHas("productionMethod", function ($query) use ($met_id) {
                    $query->whereIn('production_method_id', explode(',', $met_id));
                })->get();
            }

            $products = $products->get();

            $prod = view('partials.ajaxProducts', ['products' => $products])->render();
        }
        return response()->json(['prod' => $prod]);
    }
lztngnrs

lztngnrs2#

您可以使用:

public function ajaxResponse(Request $request) {
    if ($request->ajax()) {
        $mat_id = $request->input('mat');
        $cat_id = $request->input('cat');
        $met_id = $request->input('met');

        $products = Products::query(); 
        if ($cat_id != null) {
            $products = $products->whereHas("categories", function ($query) use ($cat_id) {
                $query->whereIn('category_id', explode(',', $cat_id));
            });
        }
        if ($mat_id != null) {
            $products = $products->whereHas("productMaterial", function ($query) use ($mat_id) {
                $query->whereIn('product_material_id', explode(',', $mat_id));
            });
        }
        if ($met_id != null) {
            $products = $products->whereHas("productionMethod", function ($query) use ($met_id) {
                $query->whereIn('production_method_id', explode(',', $met_id));
            });
        }

        $products = $products->get();

        $prod = view('partials.ajaxProducts', ['products' => $products])->render();
    }
    return response()->json(['prod' => $prod]);
}

相关问题